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 + "\t"); out.write("\r\n"); } 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 | 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 + "\t");out.write("\r\n");}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;}}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构