import java.io.*;
public class File3
{
public static void main(String[] args) throws IOException
{
char ch;
File file1=new File("E:\\JAVA\\JICHU_JAVA\\文件操作\\newFile.txt");
try{
FileInputStream fin=new FileInputStream(file1);
System.out.println("文件中的信息为:");
ch=(char) System.in.read();
while (ch !=-1)
{
System.out.print((char)ch);
ch=fin.read();
}
fin.close();
}
catch (FileNotFoundException e)
{
System.out.println(e);
}

}
}此程序编译时提示的错为
--------------------Configuration: <Default>--------------------
E:\JAVA\JICHU_JAVA\文件操作\File3.java:15: possible loss of precision
found   : int
required: char
                                ch=fin.read();
                                           ^
1 errorProcess completed.该如何修改,请指教!

解决方案 »

  1.   

    把ch定义为int 类型的
    System.out.println((char)ch);
    赶快揭帖.....................
      

  2.   

    不能读取
    newFile.txt中的信息!
      

  3.   

    int ch;while ((ch=fin.read()) !=-1)
    {
    System.out.print((char)ch);
              
    }
      

  4.   

    ch=(char) System.in.read();
    改成ch=fin.read();
      

  5.   

    因为-1是int型的,ch与-1比较会出现问题
    所以将ch定义为int类型
      

  6.   

    谢谢!
    netsummer123
    调通了,可惜
    只能读取,在记事本中的int型字符
      

  7.   

    下面代码只能读取ascii字符
    import java.io.*;
    public class File3
    {
    public static void main(String[] args) throws IOException
    {
    int ch;
    File file1=new File("D:\\JAVA\\newFile.txt");
    try{
    FileInputStream fin=new FileInputStream(file1);
    System.out.println("文件中的信息为:");
    ch= fin.read();
    while (ch!=-1)
    {
    System.out.print((char)ch);
    ch=fin.read();
    }
    fin.close();
    }
    catch (FileNotFoundException e)
    {
    System.out.println(e);
    }

    }
    }
      

  8.   

    package untitled4;
    import java.io.*;public class F
    {
            public static void main(String[] args) throws IOException
            {
                    int ch;
                    File file1=new File("F:\\enjoy\\bean.jsp");//
                    try{
                            FileReader in=new FileReader(file1);//FileInputStream fin=new FileInputStream(file1);
                            BufferedReader bufferin=new BufferedReader(in);
                            System.out.println("文件中信息为:"); //ch= fin.read();
                            String str=null;                        while((str=bufferin.readLine())!=null)//while (ch!=-1)
                            {
                                    System.out.println(str);
                            }
                    //fin.close();
                    in.close();
                    }
            catch (FileNotFoundException e){
                    System.out.println(e);
                    }
                    catch(IOException e){System.out.println(e);}        }
    }
      

  9.   

    import java.io.*;
    public class File3
    {
    public static void main(String[] args) throws IOException
    {
    int ch;
    byte[] b = new byte[256];
    File file1=new File("F:\\JavaPro\\newFile.txt");
    try{
    FileInputStream fin=new FileInputStream(file1);
    System.out.println("文件中的信息为:");
    ch= fin.read(b);
    while (ch!=-1)
    {
    String content= new String(b);
    System.out.print(content);
    ch=fin.read();
    }
    fin.close();
    }
    catch (FileNotFoundException e)
    {
    System.out.println(e);
    }

    }
    }
      

  10.   

    netsummer123(搭撒的) 你太伟大了!!!
    呵呵,我这就揭贴,感觉给您10分是有些少啊!
    昨天没来得及看您的回复,不好意思啦!感谢大家对此帖的支持!