输出地文件内容不知为何为assic码 这是我的程序
import java.io.*;
public class PrgrmStream{
public static void main (String[] args) {
try{
FileInputStream file =new FileInputStream("TestText.txt"); boolean eof = false;
int bytecount=0;
int stringcount=0;
int linecount=0;
while(!eof){
int input = file.read();
System.out.print(input);
if(input==-1)
eof=true;
else if(input==' ')
stringcount++;
else if(input=='\n')
linecount++;
else
bytecount++;
}
file.close(); //关闭文件
System.out.println("\nBytes read:"+(bytecount+1)+"\nStrings read:"+(stringcount+1)+"\nLines read:"+(linecount+1));
}
catch(IOException e){
System.out.println("Error--"+e.toString());
}
}
}

解决方案 »

  1.   

    要用BufferedReader()的readLine方法
    BufferedReader用你的fileInputStream转换成inputStreamReader初始化
      

  2.   

    嗯解决了。但是我这个程序要求统计文本文件内容的行数、字符串数、字符数,readLine()的话要怎么统计
      

  3.   

    嗯解决了。但是我这个程序要求统计文本文件内容的行数、字符串数、字符数,readLine()的话要怎么统计
      

  4.   

    int input = file.read();
    System.out.print((char) input);//input为int型变量,只有将其强制转换成字符型才行
      

  5.   

    解决了,还是read()输出的时候使用强制转换(char)就可以了,谢谢啊
      

  6.   

    public class FileOpenTest {   
        private int count = 0;   
           
      
        /**  
         *   
         * @param filePath  
         * @return read the file to record line;   
         */  
        private FileOpenTest(String filePath) {   
            File f = new File(filePath);   
            try {   
                BufferedReader br = new BufferedReader(new FileReader(f));   
      
                while (br.ready()) {   
                    br.readLine();   
                    count ++;   
                }   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
      
        private int getCount() {   
            return count;   
        }   
      
        public static void main(String[] args) {   
            String filePath = "D:\\MrTool.java";       //local file root   
            FileOpenTest fot = new FileOpenTest(filePath);   
               
            System.out.println(fot.getCount());   
        }   
           
    }