テキストファイルから読み取った要素を分割して、Javaで別々の配列に書き込もうとしています。INPUTは次のようなものです:
ID、ParentID、Name
4,17、abc
1,0、def
17,0、ghi
9,17、klm
出力は次のようになります。
ghi、17
klm、9
abc、4
def、1
IDに基づいて降順で並べ替える必要があります。私はそれを行う最も効率的な方法はクイックソートだと思います(私はこれを行う考えを持っています)。私の質問は、テキストファイルのすべての要素を分割しましたが、id、parentid、およびnameに個別の配列を作成することができません。それらが配列に分割され、IDがソートされた後、idは対応する名前を付ける必要があります。誰かが配列部分への書き込みを手伝ってくれませんか?前もって感謝します。
私はこれまで行ってきました:
import java.io.*;
public class Folder {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream fstream = new FileInputStream("input.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
String[] a=strLine.split(",",3);
String id=a[0];
String parentid=a[1];
String name=a[2];
for(int i=0;i<3;i++) {
System.out.println(a[i]);
}
//System.out.println (strLine);
}
//Close the input stream
in.close();
//Catch exception if any
}
catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
これにより、テキストファイルのすべての要素が分割されます。