2013-04-06 77 views
0

嗨我想用掃描儀替換BufferedReader中的一段代碼?我編寫了這段代碼,但後來意識到我們不允許使用bufferedreader。但是,還沒有一個線索如何甚至去,Buffered Reader Change

public static void Option1Method() throws IOException 
{ 
FileWriter aFileWriter = new FileWriter("wordlist.txt", true); 
PrintWriter out = new PrintWriter(aFileWriter); 
String word = JOptionPane.showInputDialog(null, "Enter a word"); 

out.println(word); 
out.close(); 

aFileWriter.close(); 

String inputFile = "wordlist.txt"; 
String outputFile = "wordlist.txt"; 
FileReader fileReader = new FileReader(inputFile); 
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); 

FileWriter fileWriter = new FileWriter(outputFile); 
PrintWriter out1 = new PrintWriter(fileWriter); 
for (String outputLine : lineList) { 
    out1.println(outputLine); 
} 
out1.flush(); 
out1.close(); 
fileWriter.close(); 
} 
+0

你讀過Scanner的javadoc了嗎?你不明白什麼? – 2013-04-06 10:48:39

+1

你看過[Scanner'類的Javadoc](http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)嗎?它提供了一些如何使用它的例子。 – mthmulders 2013-04-06 10:49:04

+0

所以我只是更換BufferedReader bufferedReader = new BufferedReader(fileReader);與掃描儀s =新的掃描儀(FileReader); s.findInLine ?? – user2205055 2013-04-06 10:59:23

回答

0

拿在Scanner類看看定義爲hasNextLinenextLine

雖然new Scanner(fileReader)有效,但您可以直接將文件名傳遞給Scanner的構造函數。