查看完整版本: [已解決] JFile 讀取修改內容
頁: [1]

link1943 發表於 2009-5-28 12:07 AM

[已解決] JFile 讀取修改內容

本帖最後由 link1943 於 2009-6-2 03:35 PM 編輯

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import java.io.*;

public class ModifyFile
{
private JFileChooser fileChooser;
private String fileContent = "";
private File fileChoose;
private File fileDirectory;
private String fileName;
private FileWriter fileWriter;
private Formatter output;

public ModifyFile()
{
  fileChooser = new JFileChooser();
  
}

public void openFile()
{
  int choice = fileChooser.showOpenDialog( new JFrame() );
  
  if( choice == JFileChooser.CANCEL_OPTION )
  {
   System.out.printf( "你按取消了" );
   
  }//如果選擇"取消",直接跳出
  fileChoose = fileChooser.getSelectedFile();
  
  fileDirectory = fileChooser.getCurrentDirectory();
  //System.out.print( file2 );
  try//Load file
  {
   Scanner scanner = new Scanner( fileChoose );
   while( scanner.hasNextLine() == true )
   {
    fileContent = fileContent + "\n" +  scanner.nextLine();
   }
   
   fileName = fileChoose.getName();
  }
  
  catch( FileNotFoundException fileNotFoundException )//例外
  {
   System.out.println( "Error creating file" );
  }
}

public void set_fileContent( String fileContent )
{
  this.fileContent = fileContent;
}

public String get_fileContent()
{
  return fileContent;
}

public void addRecords()
{
  try
  {
   fileWriter = new FileWriter(fileName,false);
   fileWriter.write(get_fileContent(),0,get_fileContent().length());
  }
  
  catch( IOException e )
  {
   
  }
}
}
import java.awt.BorderLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import javax.swing.JPanel;
public class Frame_ModifyFile extends JFrame
{
private JPanel Panel;
private JPanel UpPanel;
private JPanel DownPanel;
private JButton LoadButton;
private JButton SaveButton;
private JTextArea ContentArea;
private ModifyFile the_MF;

public Frame_ModifyFile() throws Exception
{
  super( "Java Drawings" );
  setLayout( new BorderLayout() );
  
  the_MF = new ModifyFile();
  Panel = new JPanel();
  UpPanel = new JPanel();
  DownPanel = new JPanel();
  LoadButton = new JButton( "讀檔" );
  SaveButton = new JButton( "存檔" );
  ContentArea = new JTextArea(15,15);
  
  LoadButton.addActionListener(
    new ActionListener()
    {
     public void actionPerformed( ActionEvent event )
     {
      the_MF.openFile();
      ContentArea.setText(the_MF.get_fileContent());
     }
    }
   );
  
  SaveButton.addActionListener(
    new ActionListener()
    {
     public void actionPerformed( ActionEvent event )
     {
      the_MF.set_fileContent(ContentArea.getText());
      System.out.printf("%s",ContentArea.getText());
      the_MF.addRecords();
      the_MF.set_fileContent("");
     }
    }
   );
  
  UpPanel.setLayout( new FlowLayout() );
  UpPanel.add( LoadButton );
  UpPanel.add( SaveButton );
  DownPanel.add( ContentArea );
  
  Panel.add( UpPanel, BorderLayout.NORTH );
  Panel.add( DownPanel, BorderLayout.SOUTH );
  add( Panel, BorderLayout.CENTER );
}
}
import javax.swing.JFrame;
public class Test_ModifyFile
{
public static void main( String[] args) throws Exception
    {
  Frame_ModifyFile application = new Frame_ModifyFile();      
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        application.setSize( 300, 300 );//預設300,300
        application.setVisible( true );
    }
}
上面是我的程式碼
我的問題是
用JFileChooser選擇檔案後
顯示在JTextArea
之後在上面做修改
之後再寫入檔案裡面
但是只要我一寫入
檔案就會變成空白

希望大家幫我看看
謝謝...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

yyyyman 發表於 2009-5-28 12:44 AM

純屬猜測,明天再來看情況  SaveButton.addActionListener(
    new ActionListener()
    {
     public void actionPerformed( ActionEvent event )
     {
      the_MF.set_fileContent(ContentArea.getText());
      System.out.printf("%s",ContentArea.getText());
      the_MF.addRecords();
      the_MF.set_fileContent(ContentArea.getText()); //改這裡試試
     }
    }
   );

yyyyman 發表於 2009-5-28 01:08 AM

另外就是我猜,你沒有使用fileWriter.flush();
強迫將buffer內的資料寫入所造成
總之你先試試看吧!

asseddie 發表於 2009-5-28 08:10 AM

樓主 53行的 the_MF.set_fileContent("");
應該是想在存檔後 把text field 變成空白吧?

至於 文字沒寫進檔案裡
有可能是 addRecords() 沒寫好?
file.write() 之後沒 file.close() 所以沒寫進去? [純粹猜測 IO的東西 我也沒試過幾次]

另一個問題
System.out.printf("%s",ContentArea.getText());
這邊顯示的結果 是正確的嘛?

link1943 發表於 2009-6-1 11:02 PM

本帖最後由 link1943 於 2009-6-2 08:27 AM 編輯

樓主 53行的 the_MF.set_fileContent("");
應該是想在存檔後 把text field 變成空白吧?

至於 文字沒寫進檔案裡
有可能是 addRecords() 沒寫好?
file.write() 之後沒 file.close() 所以沒寫進去? [純粹猜測 IO的 ...
asseddie 發表於 2009-5-28 08:10 AM http://www01.eyny.com/images/common/back.gif

嗯,53行是想要在存檔之後
將ModifyFile裡面的fileContext直接變成空白
跟你講的將textarea轉為空白,相同意思

我在addRecord裡面加寫了fileWriter.close()之後
就可以存檔了

謝謝yyyyman與asseddie的幫忙^_^...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><br><br><br><br><br><div></div>
頁: [1]