나는 다음과 같은 형식과 내용 (공간을주의 깊게 살펴)이 .txt 인 파일이 있습니다.txt 파일에서 그대로 2D 배열을 읽으려면 어떻게해야합니까?
Apples 00:00:34
Jessica 00:01:34
Cassadee 00:00:20
나는 2 차원 배열 (holder[5][2]
)로 저장 할을하고로 동시에 출력 할 수에서 JTable
. 나는 이미 자바에서 파일을 쓰고 읽는 방법을 알고 있고 그 파일을 배열에 넣는다. 그러나, 나는이 코드를 사용 : 내 holder[][]
배열이 JTable가 아주 잘 출력되지
try {
FileInputStream fi = new FileInputStream(file);
DataInputStream in = new DataInputStream(fi);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine()) != null){
for(int i = 0; i < holder.length; i++){
for(int j = 0; j < holder[i].length; j++){
holder[i][j] = line;
}
}
}
in.close();
} catch(Exception ex) {
ex.printStackTrace();
}
: | 도와주세요? 누구든지 나를 도울 수있는 덕분에!
편집 : Scanner
으로도 가능합니까? 나는 스캐너를 훨씬 잘 이해합니다.
int lineCount = 0;
int wordCount = 0;
String line = null;
while((line = br.readLine()) != null){
String[] word = line.split("\\s+");
for(String segment : word)
{
holder[lineCount][wordCount++] = segment;
}
lineCount++;
wordCount = 0; //I think now it should work, before I forgot to reset the count.
}
이 코드가 안된 있습니다,하지만 당신에게 일반적인 아이디어를 줄 것이다 : 당신이 필요합니다 무엇
당신은 = 새의 DataInputStream (FI)에'의 DataInputStream을 필요로하지 않습니다 '. 'FileInputStream'을'BufferedReader'에게 건네지는'InputStreamReader'에 직접 사용하십시오. –
@ Eng.Fouad 팁 주셔서 감사. – alicedimarco