File类下的length() 文件内容有空格的时候就会出现异常情况。
例如
FileWriter f = new FileWriter("d:\\temp.txt");
String str = "gdfgdfgdfgfdg  dddddd";
char[] c = str.toCharArray();
f.write(c);
f.close();
FileReader fr = new FileReader("d:\\temp.txt");
char[] c1 = new char["d:\\temp.txt".length()];
fr.read(c1);
fr.close();
System.out.println(new String(c1));
输出的时候只能输出gdfgdfgdfgfdg?如何解决?

解决方案 »

  1.   

    char[] c1 = new char["d:\\temp.txt".length()]; 
    应该是
    char[] c1 = new char[new File("d:\\temp.txt").length()]; 
    才对吧?
      

  2.   

    char[] c = str.toCharArray();
    f.write(c); 
    你这里c[]里的内容没有读完啊,用个循环把数组里的内容读完
      

  3.   

    这是写的代码FileWriter f = new FileWriter("d:\\temp.txt");
    String str = "gdfgdfgdfgfdg  dddddd";
    char[] c = str.toCharArray();
    for(int i=0;i<c.length;i++){
    f.write(c[i]);
    }
    f.close();
    你读的方法也有错误
      

  4.   

    这是读的方法FileReader a = new FileReader("d:\\temp.txt");
    BufferedReader b = new BufferedReader(a);
    String i = b.readLine();System.out.println(i);     a.close();
         b.close();