将CSV文件转换为Java-向后复制
我之前问过一个关于在java中将CSV文件转换为2D数组的问题。我完全重写了我的代码,它几乎是重写。我现在唯一的问题是它正在向后打印。换句话说,列在行应该在的位置打印,反之亦然。这是我的密码:将CSV文件转换为Java-向后复制,java,csv,multidimensional-array,Java,Csv,Multidimensional Array,我之前问过一个关于在java中将CSV文件转换为2D数组的问题。我完全重写了我的代码,它几乎是重写。我现在唯一的问题是它正在向后打印。换句话说,列在行应该在的位置打印,反之亦然。这是我的密码: int [][] board = new int [25][25]; String line = null; BufferedReader stream = null; ArrayList <String> csvData = new ArrayList &l
int [][] board = new int [25][25];
String line = null;
BufferedReader stream = null;
ArrayList <String> csvData = new ArrayList <String>();
stream = new BufferedReader(new FileReader(fileName));
while ((line = stream.readLine()) != null) {
String[] splitted = line.split(",");
ArrayList<String> dataLine = new ArrayList<String>(splitted.length);
for (String data : splitted)
dataLine.add(data);
csvData.addAll(dataLine);
}
int [] number = new int [csvData.size()];
for(int z = 0; z < csvData.size(); z++)
{
number[z] = Integer.parseInt(csvData.get(z));
}
for(int q = 0; q < number.length; q++)
{
System.out.println(number[q]);
}
for(int i = 0; i< number.length; i++)
{
System.out.println(number[i]);
}
for(int i=0; i<25;i++)
{
for(int j=0;j<25;j++)
{
board[i][j] = number[(j*25) + i];
}
}
int[]board=newint[25][25];
字符串行=null;
BufferedReader流=null;
ArrayList csvData=新的ArrayList();
stream=newbufferedreader(newfilereader(fileName));
而((line=stream.readLine())!=null){
String[]splitted=line.split(“,”);
ArrayList数据线=新的ArrayList(splitted.length);
用于(字符串数据:拆分)
添加(数据);
csvData.addAll(数据线);
}
int[]number=新int[csvData.size()];
对于(intz=0;z 对于(inti=0;i,如果您想要读取java中的CSV文件,那么您可能想要使用OPEN CSV
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;
public class CsvFileReader {
public static void main(String[] args) {
try {
System.out.println("\n**** readLineByLineExample ****");
String csvFilename = "C:/Users/hussain.a/Desktop/sample.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
String[] col = null;
while ((col = csvReader.readNext()) != null)
{
System.out.println(col[0] );
//System.out.println(col[0]);
}
csvReader.close();
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae+" : error here");
}catch (FileNotFoundException e)
{
System.out.println("asd");
e.printStackTrace();
} catch (IOException e) {
System.out.println("");
e.printStackTrace();
}
}
}
您可以从board[i][j]
中的Maybe swapi
和j
获取相关的jar文件?这很有效!我知道这是一个简单的错误,但我找不到。谢谢!