• java中从txt文件读取数据以及写数据到txt文件


    package com.baorant;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileWriter;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    
    public class FileUtil {
    
        /*
         * 测试用主函数
         */
        public static void main(String[] args) throws Exception {
            // File file = new File("test.txt");//数组写到文件中
            // int[] datas = {1,2,3,4,5};
            //
            // intDataToFileOut(datas, file);
    
            // 读取csv文件存到txt文件中
            // int[] recordsTotal;
            //
            // File csv = new File("csv//1203.csv"); // CSV文件路径
            // int[] records0 = GetExcelData.returnFinalArray(csv);
            // File csv1 = new File("csv//1204.csv"); // CSV文件路径
            // int[] records1 = GetExcelData.returnFinalArray(csv1);
            // File csv2 = new File("csv//1205.csv"); // CSV文件路径
            // int[] records2 = GetExcelData.returnFinalArray(csv2);
            // File csv3 = new File("csv//1206.csv"); // CSV文件路径
            // int[] records3 = GetExcelData.returnFinalArray(csv3);
            // File csv4 = new File("csv//1207.csv"); // CSV文件路径
            // int[] records4 = GetExcelData.returnFinalArray(csv4);
            // File csv5 = new File("csv//1208.csv"); // CSV文件路径
            // int[] records5 = GetExcelData.returnFinalArray(csv5);
            // File csv6 = new File("csv//1209.csv"); // CSV文件路径
            // int[] records6 = GetExcelData.returnFinalArray(csv6);
            //
            // recordsTotal = combine_two_intdata(records0,records1);
            // recordsTotal = combine_two_intdata(recordsTotal,records2);
            // recordsTotal = combine_two_intdata(recordsTotal,records3);
            // recordsTotal = combine_two_intdata(recordsTotal,records4);
            // recordsTotal = combine_two_intdata(recordsTotal,records5);
            //
            // for(int i = 0; i < recordsTotal.length; i++){
            // System.out.print(recordsTotal[i] + " ");
            // }
            //
            // File file = new File("trainData.txt");//数组写到文件中
            // intDataToFileOut(recordsTotal, file);
            //
            // File file1 = new File("testData.txt");//数组写到文件中
            // intDataToFileOut(records6, file1);
    
            //读取file中数据存到数组中
            System.out.println("获取训练数据:");
            String pathname1 = "trainData.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
            File file = new File(pathname1); // 要读取以上路径的input。txt文件
            intFileTOData(1729, file);
            //读取file中数据存到数组中
            System.out.println("获取测试数据:");
            String pathname2 = "trainData.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
            File file2 = new File(pathname2); // 要读取以上路径的input。txt文件
            intFileTOData(1729, file2);
    
        }
    
        /*
         * 读取txt中数据存到数组中
         */
        public static int[] intFileTOData(int length, File file) throws Exception {
            /* 读入TXT文件 */
            // String pathname = "trainData.txt"; //
            // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
            // File filename = new File(pathname); // 要读取以上路径的input。txt文件
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file)); // 建立一个输入流对象reader
            BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
            String line = "";
            line = br.readLine();
            int[] datas = new int[1729];
            int i = 0;
            while (line != null) {
                line = br.readLine(); // 一次读入一行数据
                System.out.println(line);
                if (null != line) {
                    int tem = Integer.valueOf(line.trim());
                    datas[i++] = tem;
                }
            }
            for (int j = 0; j < datas.length; j++) {
                System.out.println(datas[j] + " ");
            }
            return datas;
        }
    
        /*
         * 数组写到txt文件中
         */
        public static void intDataToFileOut(int[] datas, File file) throws Exception {
    
            FileWriter out = new FileWriter(file);
            for (int i = 0; i < datas.length; i++) {
                String content = datas[i] + "";
                out.write(content + "	");
                out.write("
    ");
            }
            out.close();
        }
    
        /*
         * 连接2个整形数组,得到1个整形数组
         */
        public static int[] combine_two_intdata(int a[], int b[]) {
    
            ArrayList<Integer> alist = new ArrayList<Integer>(a.length + b.length);
    
            for (int j = 0; j < a.length; j++) {
                alist.add(a[j]);
            }
    
            for (int k = 0; k < b.length; k++) {
                alist.add(b[k]);
            }
    
            int c[] = new int[alist.size()];
    
            for (int i = 0; i < alist.size(); i++) {
                c[i] = alist.get(i);
            }
            return c;
        }
    }

    数据格式:

    trainData.txt:

    802	
    1095	
    1075	
    961	
    1115	
    919	
    973	
    814	
    791	
    728	
    777	
    817	
    730	
    794	
    723	
    649	
    612	
    637	
    582	
    586	
    486	
    516	
    513	
    443	
    377	
    409	
    394	
    441	
    455	
    412	
    390	
    411	
    337	
    290	
    334	
    321	
    315	
    312	
    369	
    292	
    257	
    309	
    366	
    327	
    288	
    361	
    301	
    309	
    365	
    364	
    325	
    393	
    402	
    336	
    404	
    423	
    471	
    430	
    533	
    425	
    445	
    489	
    523	
    698	
    736	
    756	
    729	
    752	
    814	
    846	
    932	
    942	
    1180	
    1243	
    1038	
    1167	
    1386	
    1492	
    1670	
    1835	
    1935	
    2007	
    2250	
    2365	
    2688	
    2922	
    3057	
    3061	
    3366	
    3439	
    4049	
    4160	
    4576	
    4622	
    4529	
    4288	
    4295	
    4465	
    4370	
    4537	
    4672	
    4712	
    4754	
    4646	
    4772	
    5189	
    5269	
    4712	
    5052	
    4957	
    4916	
    4866	
    5232	
    4898	
    5140	
    5184	
    5515	
    5445	
    5512	
    5015	
    5154	
    5611	
    5548	
    5224	
    4903	
    5395	
    5646	
    5700	
    5660	
    5783	
    5603	
    5635	
    5586	
    5779	
    5850	
    5455	
    5960	
    5067	
    5586	
    5563	
    5283	
    5259	
    4838	
    4547	
    4345	
    4246	
    4505	
    4109	
    4316	
    4447	
    4230	
    4439	
    4736	
    4872	
    4880	
    4701	
    5344	
    5086	
    5577	
    5014	
    5696	
    5606	
    5245	
    5654	
    6116	
    5473	
    6130	
    5844	
    5783	
    5008	
    5367	
    5014	
    5490	
    5036	
    5120	
    5451	
    5073	
    4889	
    5078	
    5262	
    4937	
    5189	
    5051	
    5210	
    4959	
    5045	
    4959	
    4945	
    5096	
    4852	
    4698	
    4964	
    4712	
    5104	
    5221	
    4744	
    5499	
    5341	
    5305	
    4997	
    5808	
    5493	
    5560	
    5524	
    5618	
    5780	
    6540	
    5979	
    6194	
    6105	
    6270	
    6151	
    5757	
    5734	
    5468	
    5070	
    4542	
    4335	
    4488	
    3967	
    4332	
    3633	
    4096	
    3856	
    3643	
    3593	
    3482	
    3762	
    3484	
    3415	
    3306	
    3203	
    3392	
    3172	
    3288	
    3718	
    3555	
    3665	
    3485	
    3532	
    3339	
    3228	
    3454	
    3247	
    3267	
    3472	
    3174	
    3267	
    3148	
    3024	
    3144	
    2819	
    2701	
    2592	
    2523	
    2311	
    2321	
    2187	
    2095	
    2257	
    2294	
    2221	
    2219	
    1816	
    1908	
    1814	
    1716	
    1648	
    1516	
    1558	
    1952	
    1955	
    1626	
    1616	
    1347	
    1430	
    1391	
    1158	
    1346	
    1247	
    1078	
    1014	
    1065	
    1061	
    926	
    953	
    937	
    695	
    746	
    993	
    838	
    817	
    778	
    733	
    734	
    689	
    697	
    692	
    651	
    557	
    553	
    531	
    550	
    568	
    581	
    524	
    499	
    545	
    454	
    454	
    480	
    398	
    407	
    430	
    468	
    483	
    397	
    359	
    382	
    298	
    370	
    318	
    345	
    329	
    309	
    293	
    340	
    317	
    301	
    276	
    305	
    285	
    291	
    315	
    258	
    310	
    342	
    356	
    358	
    350	
    349	
    342	
    376	
    364	
    378	
    424	
    448	
    477	
    461	
    464	
    547	
    548	
    529	
    679	
    674	
    693	
    796	
    807	
    852	
    1017	
    1061	
    1170	
    1339	
    1447	
    1669	
    2039	
    2726	
    3164	
    4101	
    4420	
    4485	
    4564	
    4837	
    4748	
    4870	
    4838	
    5133	
    5840	
    6496	
    7007	
    7443	
    7960	
    7432	
    7078	
    6761	
    6046	
    5997	
    5868	
    5373	
    5678	
    5938	
    5353	
    5649	
    5197	
    5522	
    5625	
    5487	
    5073	
    5232	
    4936	
    5093	
    4710	
    4909	
    5051	
    5020	
    5111	
    4624	
    4871	
    4983	
    5074	
    4913	
    4628	
    4864	
    4673	
    4782	
    4794	
    4832	
    4570	
    4614	
    4703	
    4664	
    4828	
    5064	
    5141	
    4875	
    5103	
    5088	
    4931	
    5366	
    5386	
    4983	
    4610	
    4636	
    4603	
    4569	
    4184	
    4031	
    4050	
    3961	
    3783	
    3754	
    3779	
    3838	
    3899	
    3847	
    4158	
    4108	
    4260	
    4356	
    4545	
    5146	
    5156	
    5614	
    5367	
    5458	
    5256	
    4830	
    4955	
    5182	
    5176	
    5292	
    5160	
    5097	
    5008	
    4657	
    4847	
    4562	
    4514	
    4746	
    4631	
    4756	
    4596	
    4475	
    4258	
    4829	
    4879	
    4675	
    4808	
    4846	
    4707	
    4849	
    5173	
    4716	
    4810	
    5090	
    5285	
    5568	
    5742	
    5725	
    5968	
    5436	
    6284	
    5390	
    6010	
    7227	
    6562	
    5862	
    5793	
    6574	
    6611	
    5912	
    6285	
    5430	
    4971	
    5204	
    4528	
    4243	
    4174	
    3970	
    3768	
    3937	
    3463	
    3474	
    3264	
    3069	
    3314	
    3354	
    3252	
    3182	
    3182	
    3063	
    3094	
    3116	
    3075	
    3174	
    3142	
    3206	
    2860	
    3097	
    3160	
    3183	
    2974	
    3199	
    2615	
    3051	
    2927	
    2801	
    2934	
    2798	
    2532	
    2479	
    2727	
    2441	
    2545	
    2178	
    2163	
    2297	
    2312	
    2391	
    2163	
    2154	
    2107	
    1846	
    1697	
    1836	
    1607	
    1539	
    1497	
    2084	
    1918	
    1736	
    1404	
    1276	
    1347	
    1401	
    1282	
    1213	
    1069	
    977	
    910	
    923	
    1020	
    885	
    819	
    853	
    638	
    672	
    825	
    805	
    859	
    896	
    852	
    730	
    668	
    634	
    744	
    645	
    671	
    617	
    586	
    561	
    615	
    531	
    494	
    550	
    534	
    488	
    465	
    454	
    487	
    393	
    434	
    437	
    418	
    411	
    356	
    400	
    346	
    386	
    340	
    343	
    344	
    273	
    330	
    357	
    299	
    284	
    294	
    301	
    324	
    276	
    303	
    311	
    319	
    339	
    341	
    373	
    377	
    369	
    358	
    405	
    369	
    309	
    316	
    376	
    447	
    397	
    494	
    503	
    476	
    541	
    627	
    625	
    651	
    771	
    783	
    1000	
    981	
    1065	
    1147	
    1212	
    1379	
    1686	
    1763	
    2272	
    3043	
    3508	
    4067	
    4280	
    4705	
    4207	
    4495	
    4663	
    4605	
    4817	
    5348	
    6225	
    6378	
    6989	
    7844	
    7409	
    6263	
    6199	
    5723	
    5076	
    5448	
    5553	
    5433	
    5349	
    5409	
    5107	
    4878	
    5083	
    4959	
    4811	
    5426	
    5324	
    4909	
    4707	
    4983	
    4892	
    4968	
    4739	
    4913	
    4918	
    4493	
    4752	
    4692	
    5001	
    4738	
    5099	
    4200	
    4838	
    4709	
    5414	
    4849	
    4862	
    4513	
    4778	
    4834	
    4547	
    4981	
    4920	
    5099	
    5261	
    5529	
    5500	
    5079	
    4964	
    5217	
    4503	
    4370	
    4524	
    4159	
    4165	
    3891	
    3767	
    3704	
    3935	
    4139	
    3836	
    3557	
    4224	
    4042	
    4408	
    4312	
    4725	
    4179	
    4898	
    5552	
    4882	
    5500	
    5292	
    4954	
    4962	
    4839	
    5153	
    5206	
    5559	
    5452	
    5235	
    4893	
    4491	
    4947	
    5028	
    4342	
    4353	
    4321	
    4859	
    4233	
    4593	
    4736	
    4416	
    4491	
    4878	
    4715	
    4934	
    4755	
    5109	
    4665	
    5689	
    4777	
    5496	
    5566	
    5213	
    5114	
    5752	
    5439	
    5750	
    5676	
    5870	
    6783	
    6390	
    7127	
    7190	
    7040	
    6018	
    6527	
    6833	
    6609	
    5564	
    4950	
    4925	
    4446	
    4547	
    4006	
    4126	
    3960	
    3882	
    3615	
    3328	
    3319	
    3524	
    3212	
    3444	
    3276	
    3336	
    3150	
    3322	
    3329	
    3316	
    3155	
    3110	
    3259	
    3454	
    3349	
    3220	
    3114	
    3442	
    3126	
    3258	
    2896	
    3013	
    2726	
    3018	
    2870	
    2702	
    2827	
    2928	
    2701	
    2460	
    2546	
    2641	
    2172	
    2414	
    2275	
    2606	
    2029	
    1932	
    1994	
    1821	
    1754	
    1752	
    1737	
    1554	
    1695	
    2035	
    1914	
    1805	
    1441	
    1240	
    1341	
    1357	
    1252	
    1339	
    1039	
    1008	
    1066	
    993	
    935	
    954	
    900	
    870	
    500	
    703	
    891	
    902	
    846	
    774	
    811	
    854	
    763	
    729	
    685	
    704	
    578	
    651	
    565	
    604	
    484	
    467	
    478	
    493	
    430	
    466	
    455	
    459	
    432	
    354	
    421	
    408	
    398	
    405	
    400	
    368	
    386	
    351	
    351	
    351	
    292	
    294	
    317	
    339	
    340	
    318	
    312	
    315	
    299	
    321	
    268	
    329	
    362	
    389	
    387	
    355	
    376	
    424	
    322	
    384	
    381	
    405	
    459	
    431	
    435	
    446	
    458	
    471	
    537	
    567	
    558	
    661	
    705	
    776	
    893	
    886	
    1034	
    1100	
    1238	
    1226	
    1482	
    1468	
    1776	
    2429	
    2962	
    3944	
    3905	
    4064	
    4589	
    4331	
    4598	
    4673	
    4785	
    4790	
    5154	
    6425	
    6718	
    7237	
    7407	
    6758	
    7129	
    6956	
    5863	
    5714	
    5519	
    5852	
    5242	
    5956	
    5442	
    5188	
    5305	
    5247	
    4741	
    4540	
    4900	
    4825	
    4702	
    4522	
    5107	
    5164	
    4704	
    4749	
    5049	
    4783	
    4699	
    4689	
    4720	
    5023	
    4440	
    4483	
    4561	
    4259	
    4490	
    4209	
    4550	
    4740	
    4920	
    4808	
    4509	
    4745	
    4945	
    5258	
    5365	
    5119	
    5149	
    4742	
    5392	
    4823	
    4952	
    4953	
    4375	
    4516	
    4037	
    4179	
    3840	
    3782	
    3458	
    3903	
    3906	
    3906	
    3976	
    4176	
    3524	
    4303	
    4391	
    4409	
    4569	
    5109	
    5242	
    5319	
    5025	
    4823	
    5675	
    5019	
    5112	
    5376	
    4881	
    4765	
    4864	
    4441	
    4956	
    4442	
    4732	
    4080	
    4754	
    4598	
    4873	
    4458	
    4472	
    4628	
    4430	
    4342	
    4234	
    4580	
    4704	
    5357	
    5187	
    4990	
    5402	
    4885	
    4961	
    5328	
    5095	
    5215	
    5154	
    5791	
    5276	
    5448	
    5642	
    5767	
    6143	
    7010	
    6278	
    6889	
    6498	
    6820	
    5933	
    6391	
    6367	
    5274	
    4495	
    4918	
    4896	
    4526	
    4283	
    4117	
    3768	
    3567	
    3597	
    3329	
    3549	
    3608	
    3249	
    3401	
    3187	
    2979	
    3280	
    3128	
    3329	
    3245	
    3235	
    3348	
    2981	
    3340	
    3205	
    3296	
    3028	
    3066	
    2890	
    3123	
    3165	
    2989	
    3294	
    2896	
    2786	
    2738	
    2641	
    2951	
    2767	
    2653	
    2695	
    2438	
    2301	
    2441	
    2446	
    2464	
    2504	
    2292	
    2076	
    1770	
    1931	
    1893	
    1794	
    1723	
    1647	
    2133	
    1967	
    1721	
    1372	
    1239	
    1298	
    1395	
    1260	
    1183	
    1098	
    931	
    882	
    947	
    901	
    929	
    964	
    822	
    593	
    705	
    923	
    895	
    805	
    805	
    751	
    707	
    783	
    805	
    753	
    581	
    660	
    573	
    614	
    570	
    584	
    583	
    546	
    527	
    486	
    418	
    384	
    407	
    394	
    372	
    398	
    380	
    360	
    356	
    395	
    350	
    352	
    321	
    297	
    271	
    323	
    353	
    361	
    261	
    273	
    254	
    270	
    314	
    326	
    327	
    296	
    312	
    338	
    348	
    317	
    340	
    342	
    349	
    314	
    345	
    334	
    328	
    342	
    303	
    396	
    401	
    479	
    516	
    426	
    510	
    505	
    623	
    595	
    718	
    831	
    775	
    969	
    958	
    1214	
    1212	
    1295	
    1552	
    1967	
    2509	
    2955	
    3410	
    3821	
    4438	
    4195	
    4421	
    4303	
    4307	
    4749	
    4640	
    5452	
    6550	
    6297	
    6927	
    6862	
    7736	
    6574	
    6415	
    6159	
    6275	
    5751	
    5469	
    4714	
    5207	
    5478	
    5177	
    5078	
    4952	
    4982	
    4668	
    5113	
    5113	
    5015	
    4767	
    4504	
    4600	
    4626	
    4784	
    4653	
    4709	
    4846	
    4890	
    4893	
    4228	
    4721	
    4726	
    4975	
    4830	
    4725	
    4479	
    4529	
    4667	
    4722	
    4280	
    5021	
    4835	
    5026	
    4174	
    5281	
    5000	
    5341	
    5713	
    5102	
    5178	
    4756	
    4570	
    4629	
    4626	
    4166	
    3914	
    4011	
    4222	
    3993	
    4009	
    3824	
    4071	
    3947	
    3745	
    4141	
    4612	
    4131	
    4444	
    4929	
    5151	
    5518	
    5213	
    5128	
    5511	
    5508	
    4778	
    5182	
    5305	
    5143	
    5523	
    5361	
    5221	
    4519	
    4718	
    4724	
    4755	
    4416	
    4650	
    4819	
    4733	
    4492	
    4639	
    4337	
    4488	
    4374	
    4709	
    4662	
    4858	
    5165	
    4369	
    5168	
    5323	
    5351	
    5152	
    5242	
    5627	
    5688	
    5675	
    5132	
    6053	
    5562	
    6628	
    6687	
    6986	
    6724	
    6984	
    6676	
    6974	
    6263	
    5801	
    6101	
    5419	
    5265	
    4557	
    4354	
    4116	
    4222	
    4166	
    3810	
    3978	
    3416	
    3647	
    3470	
    3625	
    3342	
    3070	
    3093	
    3102	
    2960	
    3102	
    3152	
    3237	
    3246	
    3065	
    3141	
    3038	
    3126	
    3586	
    3166	
    3129	
    3121	
    3199	
    3307	
    2881	
    2554	
    2811	
    2758	
    2543	
    2627	
    2703	
    2744	
    2701	
    2325	
    2609	
    2052	
    2130	
    2343	
    2354	
    2170	
    2311	
    2129	
    1775	
    1799	
    1916	
    1707	
    1606	
    1742	
    2190	
    1830	
    1788	
    1413	
    1444	
    1280	
    1335	
    1280	
    1265	
    1157	
    1057	
    921	
    893	
    1021	
    935	
    934	
    833	
    624	
    1	
    0	
    0	
    0	
    0	
    0	
    0	
    0	
    0	
    227	
    701	
    699	
    529	
    577	
    565	
    601	
    570	
    534	
    490	
    482	
    491	
    460	
    492	
    456	
    450	
    465	
    454	
    442	
    411	
    399	
    356	
    305	
    291	
    301	
    326	
    340	
    268	
    281	
    323	
    282	
    274	
    226	
    247	
    242	
    267	
    316	
    277	
    296	
    280	
    321	
    274	
    297	
    319	
    333	
    344	
    324	
    347	
    370	
    374	
    363	
    402	
    470	
    456	
    505	
    404	
    566	
    607	
    609	
    669	
    700	
    844	
    935	
    934	
    1080	
    1039	
    1296	
    1516	
    1735	
    2233	
    3139	
    3823	
    4284	
    4182	
    4501	
    4350	
    4236	
    4409	
    4708	
    5361	
    5411	
    6280	
    6681	
    7324	
    7587	
    7508	
    7290	
    6317	
    6500	
    6116	
    6082	
    5782	
    5784	
    5540	
    5553	
    5510	
    5435	
    5312	
    5279	
    5043	
    4882	
    5183	
    5077	
    5284	
    5323	
    4322	
    1788	
    0	
    0	
    5	
    2648	
    313	
    0	
    606	
    4646	
    4832	
    4635	
    4768	
    4780	
    4584	
    4799	
    4890	
    4756	
    4733	
    5395	
    4660	
    4886	
    5226	
    5172	
    5330	
    4964	
    5317	
    5065	
    5352	
    4896	
    4748	
    4752	
    4664	
    4339	
    4213	
    4398	
    4020	
    3948	
    3686	
    3652	
    4216	
    4296	
    3943	
    4234	
    4525	
    4567	
    4813	
    4734	
    5244	
    5419	
    5534	
    5794	
    5192	
    5442	
    5298	
    5077	
    5376	
    5290	
    5927	
    5199	
    5058	
    5165	
    4722	
    4622	
    4838	
    4711	
    4620	
    4818	
    5038	
    4389	
    4946	
    4737	
    4417	
    4349	
    4978	
    4892	
    5088	
    5039	
    5123	
    5325	
    5684	
    5426	
    5753	
    5487	
    5814	
    5719	
    6129	
    5730	
    5919	
    6166	
    6485	
    6519	
    6270	
    6328	
    6347	
    6393	
    6400	
    6494	
    6663	
    6282	
    5623	
    5758	
    5173	
    4935	
    4632	
    4953	
    4681	
    4184	
    4123	
    3988	
    3864	
    3622	
    3619	
    3542	
    3295	
    3354	
    3647	
    3342	
    3527	
    3185	
    3209	
    3258	
    3192	
    3412	
    3406	
    3623	
    3354	
    3217	
    3220	
    3335	
    3324	
    3259	
    3217	
    3340	
    3213	
    3285	
    3159	
    3023	
    2951	
    2862	
    2990	
    2856	
    2585	
    2400	
    2518	
    2415	
    2477	
    2438	
    2523	
    2382	
    2043	
    1968	
    2117	
    1941	
    1757	
    1792	
    2025	
    2141	
    1993	
    1752	
    1533	
    1467	
    1471	
    1377	
    1377	
    1345	
    1250	
    1159	
    1154	
    1057	
    1056	
    1066	
    1001	
    1030	
    

      

    package com.baorant;
    import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileWriter;import java.io.InputStreamReader;import java.util.ArrayList;
    public class FileUtil {
    /* * 测试用主函数 */public static void main(String[] args) throws Exception {// File file = new File("test.txt");//数组写到文件中// int[] datas = {1,2,3,4,5};//// intDataToFileOut(datas, file);
    // 读取csv文件存到txt文件中// int[] recordsTotal;//// File csv = new File("csv//1203.csv"); // CSV文件路径// int[] records0 = GetExcelData.returnFinalArray(csv);// File csv1 = new File("csv//1204.csv"); // CSV文件路径// int[] records1 = GetExcelData.returnFinalArray(csv1);// File csv2 = new File("csv//1205.csv"); // CSV文件路径// int[] records2 = GetExcelData.returnFinalArray(csv2);// File csv3 = new File("csv//1206.csv"); // CSV文件路径// int[] records3 = GetExcelData.returnFinalArray(csv3);// File csv4 = new File("csv//1207.csv"); // CSV文件路径// int[] records4 = GetExcelData.returnFinalArray(csv4);// File csv5 = new File("csv//1208.csv"); // CSV文件路径// int[] records5 = GetExcelData.returnFinalArray(csv5);// File csv6 = new File("csv//1209.csv"); // CSV文件路径// int[] records6 = GetExcelData.returnFinalArray(csv6);//// recordsTotal = combine_two_intdata(records0,records1);// recordsTotal = combine_two_intdata(recordsTotal,records2);// recordsTotal = combine_two_intdata(recordsTotal,records3);// recordsTotal = combine_two_intdata(recordsTotal,records4);// recordsTotal = combine_two_intdata(recordsTotal,records5);//// for(int i = 0; i < recordsTotal.length; i++){// System.out.print(recordsTotal[i] + " ");// }//// File file = new File("trainData.txt");//数组写到文件中// intDataToFileOut(recordsTotal, file);//// File file1 = new File("testData.txt");//数组写到文件中// intDataToFileOut(records6, file1);
    //读取file中数据存到数组中System.out.println("获取训练数据:");String pathname1 = "trainData.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径File file = new File(pathname1); // 要读取以上路径的input。txt文件intFileTOData(1729, file);//读取file中数据存到数组中System.out.println("获取测试数据:");String pathname2 = "trainData.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径File file2 = new File(pathname2); // 要读取以上路径的input。txt文件intFileTOData(1729, file2);
    }
    /* * 读取txt中数据存到数组中 */public static int[] intFileTOData(int length, File file) throws Exception {/* 读入TXT文件 */// String pathname = "trainData.txt"; //// 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径// File filename = new File(pathname); // 要读取以上路径的input。txt文件InputStreamReader reader = new InputStreamReader(new FileInputStream(file)); // 建立一个输入流对象readerBufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言String line = "";line = br.readLine();int[] datas = new int[1729];int i = 0;while (line != null) {line = br.readLine(); // 一次读入一行数据System.out.println(line);if (null != line) {int tem = Integer.valueOf(line.trim());datas[i++] = tem;}}for (int j = 0; j < datas.length; j++) {System.out.println(datas[j] + " ");}return datas;}
    /* * 数组写到txt文件中 */public static void intDataToFileOut(int[] datas, File file) throws Exception {
    FileWriter out = new FileWriter(file);for (int i = 0; i < datas.length; i++) {String content = datas[i] + "";out.write(content + " ");out.write(" ");}out.close();}
    /* * 连接2个整形数组,得到1个整形数组 */public static int[] combine_two_intdata(int a[], int b[]) {
    ArrayList<Integer> alist = new ArrayList<Integer>(a.length + b.length);
    for (int j = 0; j < a.length; j++) {alist.add(a[j]);}
    for (int k = 0; k < b.length; k++) {alist.add(b[k]);}
    int c[] = new int[alist.size()];
    for (int i = 0; i < alist.size(); i++) {c[i] = alist.get(i);}return c;}}

  • 相关阅读:
    用原生PHP做Blog系统-Day01
    PHP做猜数字游戏
    关于html头部引用(meta,link)
    gulp基本入门
    前端构建工具gulpjs的使用介绍及技巧
    $.ajax()方法详解 jquery中的ajax方法
    js string 转 int 注意的问题——parseInt
    经常玩电脑怎么防辐射
    js 禁止重复提交
    jquery 监听回车提交
  • 原文地址:https://www.cnblogs.com/baorantHome/p/9343718.html
Copyright © 2020-2023  润新知