下面是我使用BufferedWriter将文本写入文件的代码:
public class InputOutputStream {
public static void main(String[] args)
{
FileReader f = null;
File fu = new File("C:\\Akki.txt");
BufferedReader f1 = null;
FileWriter f2 = null;
BufferedWriter f3 = null;
try{
String s1;
f = new FileReader("C:\\Test.txt");
f1 = new BufferedReader(f);
while((s1 = f1.readLine())!=null)
{
System.out.println(s1);
}
//User enters the content and same is written to file
f2 = new FileWriter(fu, true);
f3 = new BufferedWriter(f2);
System.out.println("Enter the text to write in File");
Scanner sc = new Scanner(System.in);
String k = sc.next();
f3.write(k);
f3.flush();
f3.close();
}
catch(IOException e)
{
System.out.println("IOException");
}
finally
{
if(f1!= null)
try{
f1.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}
现在,当我输入文字时:“测试数据成功写入” 只写'测试'并且不写入字符串的其余部分
请帮忙
答案 0 :(得分:3)
这不是因为作家。这是因为Scanner
。您使用next
,请尝试nextLine