Java从文件中读取2D数组,数字用逗号分隔
这是我发现的一些有助于在2D数组中读取的代码,但我遇到的问题是,这只适用于读取如下结构的数字列表:Java从文件中读取2D数组,数字用逗号分隔,java,arrays,split,2d,delimiter,Java,Arrays,Split,2d,Delimiter,这是我发现的一些有助于在2D数组中读取的代码,但我遇到的问题是,这只适用于读取如下结构的数字列表: 73 56 30 75 80 ect.. 我想要的是能够阅读以下结构的多行: 1,0,1,1,0,1,0,1,0,1 1,0,0,1,0,0,0,1,0,1 1,1,0,1,0,1,0,1,1,1 我只想将每一行作为一个数组导入,同时将它们构造为文本文件中的数组。 我读过的所有东西都说使用scan.usedelimiter(“,”);但无论我在哪里尝试使用它,程序都会直接抛出回答“错误转换数字
73
56
30
75
80
ect..
我想要的是能够阅读以下结构的多行:
1,0,1,1,0,1,0,1,0,1
1,0,0,1,0,0,0,1,0,1
1,1,0,1,0,1,0,1,1,1
我只想将每一行作为一个数组导入,同时将它们构造为文本文件中的数组。
我读过的所有东西都说使用scan.usedelimiter(“,”);但无论我在哪里尝试使用它,程序都会直接抛出回答“错误转换数字”的捕获。如果有人能帮忙,我将不胜感激。我还看到了一些关于为缓冲读取器使用split的信息,但我不知道使用/why/how哪个更好
String filename = "res/test.txt"; // Finds the file you want to test.
try{
FileReader ConnectionToFile = new FileReader(filename);
BufferedReader read = new BufferedReader(ConnectionToFile);
Scanner scan = new Scanner(read);
int[][] Spaces = new int[10][10];
int counter = 0;
try{
while(scan.hasNext() && counter < 10)
{
for(int i = 0; i < 10; i++)
{
counter = counter + 1;
for(int m = 0; m < 10; m++)
{
Spaces[i][m] = scan.nextInt();
}
}
}
for(int i = 0; i < 10; i++)
{
//Prints out Arrays to the Console, (not needed in final)
System.out.println("Array" + (i + 1) + " is: " + Spaces[i][0] + ", " + Spaces[i][1] + ", " + Spaces[i][2] + ", " + Spaces[i][3] + ", " + Spaces[i][4] + ", " + Spaces[i][5] + ", " + Spaces[i][6]+ ", " + Spaces[i][7]+ ", " + Spaces[i][8]+ ", " + Spaces[i][9]);
}
}
catch(InputMismatchException e)
{
System.out.println("Error converting number");
}
scan.close();
read.close();
}
catch (IOException e)
{
System.out.println("IO-Error open/close of file" + filename);
}
}
String filename=“res/test.txt”;//查找要测试的文件。
试一试{
FileReader ConnectionFile=新的FileReader(文件名);
BufferedReader read=新的BufferedReader(ConnectionFile);
扫描仪扫描=新扫描仪(读取);
int[][]空格=新int[10][10];
int计数器=0;
试一试{
while(scan.hasNext()&计数器<10)
{
对于(int i=0;i<10;i++)
{
计数器=计数器+1;
对于(int m=0;m<10;m++)
{
空格[i][m]=scan.nextInt();
}
}
}
对于(int i=0;i<10;i++)
{
//将阵列打印到控制台(最终不需要)
println(“数组”+(i+1)+”是:“+空格[i][0]+”、“+空格[i][1]+”、“+空格[i][2]+”、“+空格[i][3]+”、“+空格[i][4]+”、“+空格[i][5]+”、“+空格[i][6]+”、“+空格[i][7]+”、“+空格[i][8]+”、“+空格[i][9]);
}
}
捕获(输入不匹配异常e)
{
System.out.println(“错误转换编号”);
}
scan.close();
read.close();
}
捕获(IOE异常)
{
System.out.println(“文件的IO错误打开/关闭”+文件名);
}
}
下面的代码片段可能会有所帮助。基本思想是读取每一行并解析出CSV。请注意,CSV解析通常比较困难,并且大多需要专门的库(如CSVReader)。然而,手头的问题相对简单
try {
String line = "";
int rowNumber = 0;
while(scan.hasNextLine()) {
line = scan.nextLine();
String[] elements = line.split(',');
int elementCount = 0;
for(String element : elements) {
int elementValue = Integer.parseInt(element);
spaces[rowNumber][elementCount] = elementValue;
elementCount++;
}
rowNumber++;
}
} // you know what goes afterwards
因为它是一个逐行读取的文件,所以使用分隔符“,”读取每一行 因此,这里您只需使用delimter创建一个新的扫描器对象,通过每一行 代码如下所示,在第一个for循环中
for(int i = 0; i < 10; i++)
{
Scanner newScan=new Scanner(scan.nextLine()).useDelimiter(",");
counter = counter + 1;
for(int m = 0; m < 10; m++)
{
Spaces[i][m] = newScan.nextInt();
}
}
for(int i=0;i<10;i++)
{
Scanner newScan=newscanner(scan.nextLine()).useDelimiter(“,”);
计数器=计数器+1;
对于(int m=0;m<10;m++)
{
空格[i][m]=newScan.nextInt();
}
}
为什么要用扫描仪扫描文件?您已经有了一个BufferedReader
:
FileReader fileReader = new FileReader(filename);
BufferedReader reader = new BufferedReader(fileReader);
现在您可以逐行读取文件。棘手的是你需要一个int
int[][] spaces = new int[10][10];
String line = null;
int row = 0;
while ((line = reader.readLine()) != null)
{
String[] array = line.split(",");
for (int i = 0; i < array.length; i++)
{
spaces[row][i] = Integer.parseInt(array[i]);
}
row++;
}
另一件值得注意的事情是,您使用的是
int[10][10]
;这要求您提前知道文件的长度。列表将删除此要求 使用Scanner中的useDelimiter方法将分隔符设置为“”,而不是默认的空格字符。
根据给定的示例输入,如果二维数组中的下一行从新行开始,而不是使用“,”,则必须指定多个分隔符
例如:
scan.useDelimiter(",|\\r\\n");
这将分隔符设置为“,”和回车+新行字符。我在这里提供代码
public static int[][] readArray(String path) throws IOException {
//1,0,1,1,0,1,0,1,0,1
int[][] result = new int[3][10];
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
Scanner scanner = null;
line = reader.readLine();
if(line == null) {
return result;
}
String pattern = createPattern(line);
int lineNumber = 0;
MatchResult temp = null;
while(line != null) {
scanner = new Scanner(line);
scanner.findInLine(pattern);
temp = scanner.match();
int count = temp.groupCount();
for(int i=1;i<=count;i++) {
result[lineNumber][i-1] = Integer.parseInt(temp.group(i));
}
lineNumber++;
scanner.close();
line = reader.readLine();
}
return result;
}
public static String createPattern(String line) {
char[] chars = line.toCharArray();
StringBuilder pattern = new StringBuilder();;
for(char c : chars) {
if(',' == c) {
pattern.append(',');
} else {
pattern.append("(\\d+)");
}
}
return pattern.toString();
}
publicstaticint[]readArray(字符串路径)引发IOException{
//1,0,1,1,0,1,0,1,0,1
int[][]结果=新int[3][10];
BufferedReader reader=新的BufferedReader(新文件读取器(路径));
字符串行=null;
扫描器=空;
line=reader.readLine();
如果(行==null){
返回结果;
}
字符串模式=创建模式(行);
int lineNumber=0;
MatchResult temp=null;
while(行!=null){
扫描仪=新扫描仪(行);
扫描仪。findInLine(模式);
temp=scanner.match();
int count=temp.groupCount();
对于(int i=1;i为什么不将整个内容读入字符串。将字符串拆分为“,”s,然后将其转换为2D数组?在catch语句中,而不是仅打印出“错误转换数字”,请使用e.printStackTrace()或System.out.println(e)。这样你就可以真正看到错误是什么。你的问题解决了吗???非常感谢你的快速回复。缓冲读取器代码段工作完美,更有意义。我唯一的另一个问题是,“使用缓冲读取器与扫描仪相比有什么好处?”
public static int[][] readArray(String path) throws IOException {
//1,0,1,1,0,1,0,1,0,1
int[][] result = new int[3][10];
BufferedReader reader = new BufferedReader(new FileReader(path));
String line = null;
Scanner scanner = null;
line = reader.readLine();
if(line == null) {
return result;
}
String pattern = createPattern(line);
int lineNumber = 0;
MatchResult temp = null;
while(line != null) {
scanner = new Scanner(line);
scanner.findInLine(pattern);
temp = scanner.match();
int count = temp.groupCount();
for(int i=1;i<=count;i++) {
result[lineNumber][i-1] = Integer.parseInt(temp.group(i));
}
lineNumber++;
scanner.close();
line = reader.readLine();
}
return result;
}
public static String createPattern(String line) {
char[] chars = line.toCharArray();
StringBuilder pattern = new StringBuilder();;
for(char c : chars) {
if(',' == c) {
pattern.append(',');
} else {
pattern.append("(\\d+)");
}
}
return pattern.toString();
}