java中如何将数据从键盘输入并写入文件,再读出来
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文件中,可以看出你要添加的字符
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文件中,可以看出你要添加的字符
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询