简单输入问题,高手肯定遇到过。

nifly 2005-03-22 11:59:54
我已经写了一段从键盘输入然后写到F.txt这个文件中,但是这个只支持E文字符啊。如下
======================================================
import java.io.*;
public class AccessFile
{
public static void main(String[] args)
{
try{
FileWriter f=new FileWriter("F.txt",true);
DataInputStream x=new DataInputStream(System.in);
String d=x.readLine();
d=d+"\n";
f.write(d);
f.flush();

}
catch(FileNotFoundException e)
{}
catch(IOException e)
{}
}
}
============================================
请问如何才能使用中文输入并能保存到文件中呢?
请写出代码~~非常感谢。
...全文
108 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
feng2008ao 2005-03-22
  • 打赏
  • 举报
回复
对,楼上的这段很重要,就这个就可以了。
dutzzl 2005-03-22
  • 打赏
  • 举报
回复
This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));
cuilichen 2005-03-22
  • 打赏
  • 举报
回复
这是因为System.in流,你直接存入了*.txt文件。
你可以使用awt做一个接受输入的控件,这样就可以接受中文了。
再加一个提交的按钮,调用写文件的函数。
nifly 2005-03-22
  • 打赏
  • 举报
回复
OHYES感谢~~揭贴各位明白老~挖哈哈
Tomcat4 2005-03-22
  • 打赏
  • 举报
回复
DataInputStream只处理字节流,而BufferedReader处理字符流。中文是双字节的UNICODE码。
nifly 2005-03-22
  • 打赏
  • 举报
回复
你好啊~我换过来了可以行得通
但是请用中文解释一下好吗??
非常地感谢啊~

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧