如何用Java将文本写入文件

时间:2022-03-21 14:03:53

I'm new to java and currently trying to write some strings into a text file using this tut : http://www.homeandlearn.co.uk/java/write_to_textfile.html

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

so here is my code :

所以这是我的代码:

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

and this is how I call the method:

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

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

But nothing appears in the file! I'm really sick of this; what did I do wrong?

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

5 个解决方案

#1


2  

The code is fine.

代码很好。

You should be looking in the correct file. If you run the file in Eclipse or Netbeans, the created text file is located in your project directory.

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

#2


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

#3


1  

The code works perfectly. Test code:

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

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

#4


0  

In order to handle files,there are different kinds of Java classes like character streams and bytes stream. Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.All character stream classes are descended from Reader and Writer. There are different style to write to text file.So better to follow this link http://www.tutorialspoint.com/java/java_files_io.htm.Happy Coding

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

#5


0  

I tried the following code and its working fine. Please provide the exact error message u r getting

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

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

}

#1


2  

The code is fine.

代码很好。

You should be looking in the correct file. If you run the file in Eclipse or Netbeans, the created text file is located in your project directory.

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

#2


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

#3


1  

The code works perfectly. Test code:

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

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

#4


0  

In order to handle files,there are different kinds of Java classes like character streams and bytes stream. Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.All character stream classes are descended from Reader and Writer. There are different style to write to text file.So better to follow this link http://www.tutorialspoint.com/java/java_files_io.htm.Happy Coding

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

#5


0  

I tried the following code and its working fine. Please provide the exact error message u r getting

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

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

}