簡體   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