繁体   English   中英

如何用Java将文本写入文件

[英]How to write text to a file in Java

我是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());
}

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

代码很好。

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

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) {}
        }
    }
}

该代码运行完美。 测试代码:

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());
    }
    }
}

为了处理文件,存在各种Java类,例如字符流和字节流。 程序使用字节流执行8位字节的输入和输出。 所有字节流类均来自InputStream和OutputStream。所有字符流类均来自Reader和Writer。 写文本文件有不同的样式,因此最好遵循此链接http://www.tutorialspoint.com/java/java_files_io.htm

我尝试了以下代码及其正常工作。 请提供您得到的确切错误消息

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();
  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM