package cn.com.process;import java.io.*;public class FileInputStream {
public static void main(String args[]) throws IOException {

char c;
File f = new File("d://c.txt");
    FileOutputStream fw = new FileOutputStream(f);
        //String name="English";
String name = "我要输入中文!";//输入中文为什么是乱码?如何处理?
for(int i=0;i<name.length();i++){
c=name.charAt(i);
fw.write(c);
}
fw.flush();
fw.close();
BufferedReader reader = null;
try {
FileReader fr = new FileReader(f);
reader = new BufferedReader(fr);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String s = null; s = reader.readLine();
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
System.out.print(c);
}
reader.close(); }

解决方案 »

  1.   

    请用FileReader   它是字符流    FileOutputStream  是字节流   所以读出来就是乱码.
      

  2.   

    把name 转换为byte类型的,再写入流中import java.io.*; public class Test { 
    public static void main(String args[]) throws IOException {  File f = new File("d://c.txt"); 
    FileOutputStream fw = new FileOutputStream(f); 
            //String name="English"; 
    String name = "我要输入中文!";//输入中文为什么是乱码?如何处理? 
    byte[] on = name.getBytes(); 
    fw.write(on,0,on.length); 
    fw.flush(); 
    fw.close(); 
    BufferedReader reader = null; 
    try { 
    FileReader fr = new FileReader(f); 
    reader = new BufferedReader(fr); 
    String s = null; 

    s = reader.readLine(); 
    System.out.print(s);
    reader.close();
    } catch (FileNotFoundException e) { 
    e.printStackTrace();
    }
    }
    }
      

  3.   

    别转换了,累不累啊。
    直接用PrintWriter out = new PrintWriter(new FileOutputStream("xxx.txt"));
    out.println("中文滴");就这样,想写啥直接往里整就行了,字符就用字符流搞哦。
      

  4.   

    1.5以后不都推荐用printwriter了嘛
      

  5.   

    谢谢大家,我是得好好看看java的字符流和字节流了,之前没有好好看。