我正在嘗試使用android對文本文件中的數字進行排序。你可以幫我嗎?這段代碼不是全部排序的部分。我的意思是它排序1.A 2.C 20.D 3.C 4.A 5.A那樣。它應該是1.A 2.C 3.C 4.A 5.A 6.B 20.D我需要轉換整數,但我該怎麼做?在文本文件中對數字進行排序android
嘗試 {
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists())
{
root.mkdirs();
}
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
writer.append(s+"\n\n");
writer.flush();
writer.close();
FileReader fileReader = new FileReader(gpxfile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String inputLine;
List<String> lineList = new ArrayList<String>();
while ((inputLine = bufferedReader.readLine()) != null) {
lineList.add(inputLine);
}
fileReader.close();
Collections.sort(lineList);
System.out.println(lineList);
FileWriter fileWriter = new FileWriter(gpxfile);
PrintWriter out = new PrintWriter(fileWriter);
for (String outputLine : lineList) {
out.println(outputLine);
}
out.flush();
out.close();
fileWriter.close();
//Toast.makeText(this, "Data has been written to Report File", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
}
你在哪裏排序數字? – Mohit
@Mohit在文本文件中,我應該排序。 Collections.sort我使用它,但它排序爲一個字符串,因此1. 2. 20. 3 ...不是1. 2. 3. 20但我該怎麼做? – xiac
嘗試使用'Set'來代替 – Mohit