java中如何将数据从键盘输入并写入文件,再读出来

2024-12-13 21:37:13
推荐回答(1个)
回答1:

public class FileDemo {
public static void main(String[] args) throws Exception
{
PrintWriter wi=new PrintWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),"gbk"));

wi.print(new Scanner(System.in).nextLine());
wi.close();
BufferedReader re=new BufferedReader(new InputStreamReader(new FileInputStream("file.txt"),"gbk"));
String ss=null;
if((ss=re.readLine())!=null)
{
System.out.println(ss);
}
re.close();
}
}
在控制台输入你需要添加的字符,将字符写入file.txt文件中,可以看出你要添加的字符