- //从键盘输入一行文字,写入文件中
package project07;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestFileOut {
public static void main(String[] args) {
char c[] = new char[512];
int n,i;
try {
FileWriter wf = new FileWriter("D:/result.txt");
//利用InputStreamReader正确读取中文
System.out.println("请输入中文:");
InputStreamReader isr = new InputStreamReader(System.in);
n = isr.read(c,0,512);//一次性读取512个字符,n表示实际读取的字符数
wf.write(c);
wf.close();
System.out.println("刚刚输入的数据为:"+String.valueOf(c,0,n));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}
![这里写图片描述](https://img-blog.csdn.net/20171224173405865?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzkwNDY3Mjc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)