File file = new File("Skill.txt");
Scanner new_sc;
try {
new_sc = new Scanner(file);
while (new_sc.hasNextLine())
System.out.println(new_sc.nextLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
我使用了try catch方法,我不熟悉这种方法。
答案 0 :(得分:0)
您的代码用于从文件中读取。但是如果你想创建,写入和读取文件:
您可以尝试这样的事情:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Helper {
public static void main(String[] args) {
File fout = new File("Skill.txt");
FileOutputStream fos;
try {
//Writing to the file
fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write("Writing to the file ...");
bw.newLine();
bw.close();
//Reading from file
Scanner new_sc = new Scanner(fout);
while (new_sc.hasNextLine())
System.out.println(new_sc.nextLine());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}