Java 从.txt读入二维数组
对不起,如果我的代码看起来不好,我在编程方面没有那么丰富的经验。我需要以:Date-Name-Address-etc.的格式从.txt传输文本 我正在读取文件,然后用string.split(“-”)拆分字符串。我的线圈有问题Java 从.txt读入二维数组,java,arrays,file,multidimensional-array,Java,Arrays,File,Multidimensional Array,对不起,如果我的代码看起来不好,我在编程方面没有那么丰富的经验。我需要以:Date-Name-Address-etc.的格式从.txt传输文本 我正在读取文件,然后用string.split(“-”)拆分字符串。我的线圈有问题 try{ File file = new File("testwrite.txt"); Scanner scan = new Scanner(file); String[] test = scan.nextLine(
try{
File file = new File("testwrite.txt");
Scanner scan = new Scanner(file);
String[] test = scan.nextLine().split("-");
while(r<100){
while(c<6){
data[r][c] = test[c];
test = scan.nextLine().split("-");
c++;
}
r++;
c = 0 ;
}
System.out.println(data[1][5]);
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
试试看{
File File=新文件(“testwrite.txt”);
扫描仪扫描=新扫描仪(文件);
String[]test=scan.nextLine().split(“-”);
while(r看起来您调用scan.nextLine()的次数太多了。每次调用scan.nextLine(),它都会使扫描仪超过当前行。假设您的文件有100行,每行有6个“条目”(以“-”分隔),我会将test=scan.nextLine().split(“-”;
移动到while循环的末尾(但仍在循环中)所以每行调用一次
编辑
提议的解决办法:
在表单中给定一个文件
a-b-c-x-y-z
a-b-c-x-y-z
…(总共100倍)
使用此代码:
try{
File file = new File("testwrite.txt");
Scanner scan = new Scanner(file);
String[] test = scan.nextLine().split("-");
while(r<100){
while(c<6){
data[r][c] = test[c];
c++;
}
r++;
c = 0 ;
test = scan.nextLine().split("-");
}
System.out.println(data[1][5]);
}catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
试试看{
File File=新文件(“testwrite.txt”);
扫描仪扫描=新扫描仪(文件);
String[]test=scan.nextLine().split(“-”);
而(rI)使用以下方法拆分选项卡分隔的文件:
BufferedReader reader = new BufferedReader(new FileReader(path));
int lineNum = 0; //Use this to skip past a column header, remove if you don't have one
String readLine;
while ((readLine = reader.readLine()) != null) { //read until end of stream
if (lineNum == 0) {
lineNum++; //increment our line number so we start with our content at line 1.
continue;
}
String[] nextLine = readLine.split("\t");
for (int x = 0; x < nextLine.length; x++) {
nextLine[x] = nextLine[x].replace("\'", ""); //an example of using the line to do processing.
...additional file processing logic here...
}
}
BufferedReader reader=newbufferedreader(newfilereader(path));
int lineNum=0;//使用此选项跳过列标题,如果没有,则删除
字符串读取线;
而((readLine=reader.readLine())!=null){//一直读到流结束
如果(lineNum==0){
lineNum++;//增加行号,以便从第1行开始内容。
继续;
}
字符串[]nextLine=readLine.split(“\t”);
对于(int x=0;x
同样,在我的例子中,我是在制表符(\t)上拆分,但除了新行字符外,您也可以轻松地拆分-或任何其他字符
根据一行被认为是由换行符('\n')、回车符('\r')或紧接着换行符的回车符中的任何一个终止的。
一旦按需要将行拆分,只需根据需要将其分配到数组。二维数组就是“数组数组数组”,因此您可以直接使用split
result来存储一行的数据
File file = new File("testwrite.txt");
Scanner scanner = new Scanner(file);
final int maxLines = 100;
String[][] resultArray = new String[maxLines][];
int linesCounter = 0;
while (scanner.hasNextLine() && linesCounter < maxLines) {
resultArray[linesCounter] = scanner.nextLine().split("-");
linesCounter++;
}
File File=new文件(“testwrite.txt”);
扫描仪=新扫描仪(文件);
最终整数最大线=100;
字符串[][]结果数组=新字符串[maxLines][];
int linesconter=0;
while(scanner.hasNextLine()&&linesconter
System.out.println(数据[1][5])-用于测试目的。看起来像for
-循环更适合于此。首先,第8行,test=scan.nextLine().split(“-”);test是字符串数组,您需要指定索引。如果您提供更多详细信息,我很乐意提供帮助。请编辑您的问题。