如题所述
第1个回答 2016-10-11
public class A {
public static void main(String[] args) {
File file = new File("a.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(file.getAbsolutePath());
try {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
int num = 1;
StringBuilder stringBuilder = new StringBuilder();
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
stringBuilder.append(line).append("\n");
System.out.println(line);
}
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(stringBuilder.toString().replaceFirst("\n", "tag\n"));
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
File file = new File("a.txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(file.getAbsolutePath());
try {
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
int num = 1;
StringBuilder stringBuilder = new StringBuilder();
while (bufferedReader.ready()) {
String line = bufferedReader.readLine();
stringBuilder.append(line).append("\n");
System.out.println(line);
}
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(stringBuilder.toString().replaceFirst("\n", "tag\n"));
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第2个回答 2016-10-11
writer.write(str, true).str是要写入的内容,传递true表示不覆盖,追加在文件末尾本回答被网友采纳
第3个回答 2018-05-07
writer.write(str, true).str是要写入的内容,传递true表示不覆盖,追加在文件末尾