我是java和使用netbeans v8的新手。我试图从文件中读取字符串并将修改保存在新文件中,但当我运行代码时,它不显示任何继续运行的内容。代码是
package filereadingandwriting;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileReadingAndWriting {
public static void readAndWrite() throws Exception
{
File inputFile = new File("names.txt");
Scanner input = new Scanner(System.in);
File outFile = new File("output.txt");
PrintWriter writer = new PrintWriter(outFile);
for(int i = 0;input.hasNextLine(); i++)
{
String inputLines = input.nextLine();
writer.println("Sr#" + i + " " + inputLines);
}
input.close();
} // readAndWrite function ends.
public static void main(String[] args) throws Exception {
// TODO code application logic here
readAndWrite();
} // main function ends.
} //FileReadingAnd Writing class ends.
如果你想从你的inputFile中读取,你必须修改你的代码:
File inputFile = new File("names.txt");
Scanner input = new Scanner(inputFile);
在您的情况下,您将从STDIN读取。
您还应该使用较新的java.nio.*
API
将new Scanner(System.in);
改为new Scanner(inputFile);