如何用Java将文本写入文件

时间:2014-06-15 10:57:08

标签: java filewriter

我是java新手,目前正尝试使用此tut将一些字符串写入文本文件: http://www.homeandlearn.co.uk/java/write_to_textfile.html

所以这是我的代码:

public void savefile() throws IOException {      
    JOptionPane.showMessageDialog(null,"Hi,i'm in Try Block :|");
    FileWriter write = new FileWriter("asd.txt", true);
    PrintWriter print = new PrintWriter(write);
    JOptionPane.showMessageDialog(null, "File Opened");
    write.write("Knock Knock");
    print.flush();
    print.write("Hello ?");
    print.flush();
    print.printf("Hi?");
    print.flush();
    print.println("anybody there?");
    print.flush();
    JOptionPane.showMessageDialog(null, "Can you hear me ?");
    print.close();
    JOptionPane.showMessageDialog(null, "File Closed");         
}

这就是我调用方法的方法:

try {
    savefile();
}
catch (IOException e) {
    JOptionPane.showMessageDialog(null, "Error: " + e.getMessage());
}

但文件中没有任何内容!我真的厌倦了这个;我做错了什么?

5 个答案:

答案 0 :(得分:2)

代码很好。

您应该查看正确的文件。如果在Eclipse或Netbeans中运行该文件,则创建的文本文件位于项目目录中。

答案 1 :(得分:1)

public void saveFile()
{
    BufferedWriter bufferedWriter = null;
    try
    {
        bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("asd.txt"))));
        bufferedWriter.writeLine("Hello world!");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(bufferedWriter != null)
        {
            try
            {
                bufferedWriter.close();
            }
            catch(IOException e) {}
        }
    }
}

答案 2 :(得分:1)

代码完美无缺。 测试代码:

public class ij3
{
    public void savefile() throws IOException 
    {      
        JOptionPane.showMessageDialog(null,"Hi,i'm in Try Block :|");
        FileWriter write = new FileWriter("asd.txt",true);
        PrintWriter print = new PrintWriter(write);
        JOptionPane.showMessageDialog(null,"File Opened");
        write.write("Knock Knock");
        print.flush();
        print.write("Hello ?");
        print.flush();
        print.printf("Hi?");
        print.flush();
        print.println("anybody there?");
        print.flush();
        JOptionPane.showMessageDialog(null,"Can you hear me ?");
        print.close();
        JOptionPane.showMessageDialog(null,"File Closed");         
    }
public static void main(String [] args)
{
    ij3 s = new ij3();      
    try
    {
            s.savefile();
        }
        catch (IOException e){
        JOptionPane.showMessageDialog(null,"Error: "+e.getMessage());
    }
    }
}

答案 3 :(得分:0)

为了处理文件,有不同种类的Java类,如字符流和字节流。程序使用字节流来执行8位字节的输入和输出。所有字节流类都来自InputStream和OutputStream.All字符流类来自Reader和Writer。写入文本文件有不同的样式。更好地关注此链接http://www.tutorialspoint.com/java/java_files_io.htm。快乐编码

答案 4 :(得分:0)

我尝试了以下代码并且工作正常。请提供准确的错误消息

public class Writer {

/**
 * @param args
 */
public static void main(String[] args) {
    try {
        new Writer().savefile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public void savefile() throws IOException {      
    FileWriter write = new FileWriter("d:/asd.txt");
    PrintWriter print = new PrintWriter(write);
   write.write("Knock Knock");
    print.flush();
    print.write("Hello ?");
    print.flush();
    print.printf("Hi?");
    print.flush();
    print.println("anybody there?");
    print.flush();
    print.close();
  }

}