linfaimom
来撸一发吧
2015-06-04 05:22
采纳率: 33.3%
浏览 1.8k
已采纳

java菜鸟真心求教大神,谢谢!

请大神看看下面的代码,我检查了好几次都没发现错误,为何在编译的时候会出现java找不到符号的情况。。。。代码可能有点长。。。麻烦各位了,谢谢!

 //测试FileWriter类

public class TestFileWriter
{
    public static void showFile(java.io.File file)
    {
        char buff[] = new char[22];
        int ret = 0;

        System.out.println("Showfile begin["+file.getName()+"]:");

        java.io.FileReader read_file = null;

        try
        {
            //创建读取文件对象
            read_file = new java.io.FileReader(file);

            do
            {
                //读取一段字符,每次22个字符
                ret = read_file.read(buff);  

                for (int i=0; i<ret; i++)
                {
                    System.out.print(buff[i]);  //逐个输出读入的字符
                }
            }
            while (ret!=-1);
        }
        catch (java.lang.Exception e)
        {
            System.out.println("There must be something wrong!");
        }
        finally
        {
            //关闭打开的文件
            try
            {
                read_file.close();
            }
            catch (java.lang.Exception e)
            {
                System.out.println("Ignore the exception when closing.");
            }
        }

        System.out.println("\nshowfile end ["+file.getName()+"].");
    }


    public static void main(String[] argv)
    {
        char buff[] = new char[48];
        char ch = 'A';
        int ret = 0;

        for (int i=0; i<26; i++)
        {
            buff[i] = ch++;  //输入A-Z共26个字母到字符数组buff
        }

        java.io.File _write = new java.io.File("_write.txt");
        java.io.FileWriter write_java = null;

        try
        {
            System.out.println(_write.getName()+"exist?"+_write.exists());

            write_java = new java.io.FileWriter("_write.txt");  //创建写入文本的对象

            System.out.println(_write.getName()+"exist?"+_write.exists());

            write_java.write(buff,0,26);  //将buff数组里的内容输出到指定文件

            System.out.println(_write.getName()+"exist?"+_write.exists());

            //write_java.flush();

            write_java.close();  //调用close()会自动执行flush(),如果将这一行放在show_file()方法后面,则show_file()显示文件为空,因为未写入

            System.out.println("\nWrite into an empty file.");

            showFile(_write);  //调用show_file()方法
        }
        catch (java.lang.Exception e)
        {
            System.out.println("There must be something wrong!");
        }
        finally
        {
            try
            {
                write_java.close();
            }
            catch (java.lang.Exception e)
            {
                System.out.println("Ignore the exception when closing.");
            }

            java.io.FileWriter append_java = null; 

        }

        try
        {
            //使用append()方法向文件追加内容
            append_java = new java.io.FileWriter(_write);  //以普通形式打开,结果依然覆盖原文
            append_java.append("Someting appended in the file....");
            append_java.close();

            System.out.println("\n以普通方式打开,调用append()方法");  
            showFile(_write);
        }
        catch (java.lang.Exception e)
        {
            System.out.println("There must be something wrong!");
        }
        finally
        {
            try
            {
                append_java.close();
            }
            catch (java.lang.Exception e)
            {
                System.out.println("Ignore the exception when closing.");
            }
        }

        try
        {
            //以追加方式打开文件,并使用append()方法向文件追加内容

            append_java = new java.io.FileWriter(_write, true);
            append_java.write(buff,0,26);
            append_java.close();

            System.out.println("\n以追加模式打开文件,用append()方法追加内容到文件中");
            showFile(_write);
        }
        catch (java.lang.Exception e)
        {
            System.out.println("There must be something wrong!");
        }
        finally
        {
            try
            {
                append_java.close();
            }
            catch (java.lang.Exception e)
            {
                System.out.println("Ignore the exception when closing.");
            }
        }

        try
        {
            //以非追加方式打开,并用append()方法向文件追加内容
            append_java = new java.io.FileWriter(_write, false);
            append_java.append(new String(buff).trim());
            append_java.close();
            System.out.println("\n以非追加方式打开,调用append()方法");
            showFile(_write);
        }
        catch (java.lang.Exception e)
        {
            System.out.println("There must be something wrong!");
        }
        finally
        {
            try
            {
                append_java.close();
            }
            catch (java.lang.Exception e)
            {
                System.out.println("Ignore the exception when closing.");
            }
        }

        _write.delete();
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • danielinbiti
    danielinbiti 2015-06-04 05:25
    已采纳
     finally
            {
                try
                {
                    write_java.close();
                }
                catch (java.lang.Exception e)
                {
                    System.out.println("Ignore the exception when closing.");
                }
    
                java.io.FileWriter append_java = null; 
    
            }
    
            try
            {
                //使用append()方法向文件追加内容
                append_java = new java.io.FileWriter(_write);  //以普通形式打开,结果依然覆盖原文
                append_java.append("Someting appended in the file....");
                append_java.close();
    
                System.out.println("\n以普通方式打开,调用append()方法");  
                showFile(_write);
            }
                    改成
                    finally
            {
                try
                {
                    write_java.close();
                }
                catch (java.lang.Exception e)
                {
                    System.out.println("Ignore the exception when closing.");
                }
    
    
            }
    
                java.io.FileWriter append_java = null; //把这个变量申明拿出来
            try
            {
                //使用append()方法向文件追加内容
                append_java = new java.io.FileWriter(_write);  //以普通形式打开,结果依然覆盖原文
                append_java.append("Someting appended in the file....");
                append_java.close();
    
                System.out.println("\n以普通方式打开,调用append()方法");  
                showFile(_write);
            }
    
    点赞 评论
  • q3733353520

    另外下次提问的时候可以把错误信息也一起贴出来

    点赞 评论
  • tongyi55555
    JonsonJiao 2015-06-04 05:32

    这些问题只要在IDE编译器里都会很快解决的,我放到我的Eclipse里面直接就提示了,你的append_java没有声明对象,声明一下就可以了。看代码:

    public class TestFileWriter {
        public static void showFile(java.io.File file) {
            char buff[] = new char[22];
            int ret = 0;
    
            System.out.println("Showfile begin[" + file.getName() + "]:");
    
            java.io.FileReader read_file = null;
    
            try {
                // 创建读取文件对象
                read_file = new java.io.FileReader(file);
    
                do {
                    // 读取一段字符,每次22个字符
                    ret = read_file.read(buff);
    
                    for (int i = 0; i < ret; i++) {
                        System.out.print(buff[i]); // 逐个输出读入的字符
                    }
                } while (ret != -1);
            } catch (java.lang.Exception e) {
                System.out.println("There must be something wrong!");
            } finally {
                // 关闭打开的文件
                try {
                    read_file.close();
                } catch (java.lang.Exception e) {
                    System.out.println("Ignore the exception when closing.");
                }
            }
    
            System.out.println("\nshowfile end [" + file.getName() + "].");
        }
    
        public static void main(String[] argv) {
            char buff[] = new char[48];
            char ch = 'A';
            int ret = 0;
    
            for (int i = 0; i < 26; i++) {
                buff[i] = ch++; // 输入A-Z共26个字母到字符数组buff
            }
    
            java.io.File _write = new java.io.File("_write.txt");
            java.io.FileWriter write_java = null;
    
            try {
                System.out.println(_write.getName() + "exist?" + _write.exists());
    
                write_java = new java.io.FileWriter("_write.txt"); // 创建写入文本的对象
    
                System.out.println(_write.getName() + "exist?" + _write.exists());
    
                write_java.write(buff, 0, 26); // 将buff数组里的内容输出到指定文件
    
                System.out.println(_write.getName() + "exist?" + _write.exists());
    
                // write_java.flush();
    
                write_java.close(); // 调用close()会自动执行flush(),如果将这一行放在show_file()方法后面,则show_file()显示文件为空,因为未写入
    
                System.out.println("\nWrite into an empty file.");
    
                showFile(_write); // 调用show_file()方法
            } catch (java.lang.Exception e) {
                System.out.println("There must be something wrong!");
            } finally {
                try {
                    write_java.close();
                } catch (java.lang.Exception e) {
                    System.out.println("Ignore the exception when closing.");
                }
    
                java.io.FileWriter append_java = null;
    
            }
    
            FileWriter append_java = null;
            try {
                // 使用append()方法向文件追加内容
                append_java = new java.io.FileWriter(_write); // 以普通形式打开,结果依然覆盖原文
                append_java.append("Someting appended in the file....");
                append_java.close();
    
                System.out.println("\n以普通方式打开,调用append()方法");
                showFile(_write);
            } catch (java.lang.Exception e) {
                System.out.println("There must be something wrong!");
            } finally {
                try {
                    append_java.close();
                } catch (java.lang.Exception e) {
                    System.out.println("Ignore the exception when closing.");
                }
            }
    
            try {
                // 以追加方式打开文件,并使用append()方法向文件追加内容
    
                append_java = new java.io.FileWriter(_write, true);
                append_java.write(buff, 0, 26);
                append_java.close();
    
                System.out.println("\n以追加模式打开文件,用append()方法追加内容到文件中");
                showFile(_write);
            } catch (java.lang.Exception e) {
                System.out.println("There must be something wrong!");
            } finally {
                try {
                    append_java.close();
                } catch (java.lang.Exception e) {
                    System.out.println("Ignore the exception when closing.");
                }
            }
    
            try {
                // 以非追加方式打开,并用append()方法向文件追加内容
                append_java = new java.io.FileWriter(_write, false);
                append_java.append(new String(buff).trim());
                append_java.close();
                System.out.println("\n以非追加方式打开,调用append()方法");
                showFile(_write);
            } catch (java.lang.Exception e) {
                System.out.println("There must be something wrong!");
            } finally {
                try {
                    append_java.close();
                } catch (java.lang.Exception e) {
                    System.out.println("Ignore the exception when closing.");
                }
            }
    
            _write.delete();
        }
    }
    
    
    点赞 评论
  • bhdgx
    Soulic 2015-06-04 05:36

    append _java 变量申明的地方错了,放到finally语句块以外就可以了

    点赞 评论
  • linfaimom
    来撸一发吧 2015-06-04 05:46

    恩,我是照着书本打的,刚开始我也觉得疑惑,为啥声明一个对象会放在finally里面。。。自己也试过把它拿出来另起一行,可能当时也没写对位置吧,总之谢谢啦

    点赞 评论

相关推荐