2015-03-25 126 views
1

我正在製作一個小型gui,其中我從文本文件讀取數據到不同的JComboBoxes,JComboBoxes中的用戶編輯,然後將其保存在新的文本文件中。我的新文件被保存但不是格式從它被讀取。 代碼:將JComboBoxes值保存到文本文件

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.io.*; 
import java.util.*; 
import java.awt.event.*; 

public class A extends JPanel 
{ 
public A() { 

JPanel buttonPanel = new JPanel(); 
add(buttonPanel); 
buttonPanel.setLayout(new GridLayout(0, 2, 10, 10)); 


JComboBox combo1 = new JComboBox(); 
combo1.addItem("1ms"); 
combo1.addItem("2ms"); 
buttonPanel.add(combo1); 

JComboBox combo2 = new JComboBox(); 
combo2.addItem("1ms"); 
combo2.addItem("2ms"); 
buttonPanel.add(combo2); 

JComboBox combo3 = new JComboBox(); 
combo3.addItem("1ms"); 
combo3.addItem("2ms"); 
buttonPanel.add(combo3); 

JComboBox combo4 = new JComboBox(); 
combo4.addItem("1ms"); 
combo4.addItem("2ms"); 
buttonPanel.add(combo4); 


JComboBox[] fieldBoxs = new JComboBox[4]; 
fieldBoxs[0] = combo1; 
    fieldBoxs[1] = combo2; 
    fieldBoxs[2] = combo3; 
    fieldBoxs[3] = combo4; 


ArrayList<String> list = new ArrayList<String>(); 

final StringBuilder temp = new StringBuilder(); 

try{ 
    InputStream ips=new FileInputStream("test.txt"); 
     InputStreamReader ipsr=new InputStreamReader(ips); 
     BufferedReader br=new BufferedReader(ipsr); 

      String line; 
    boolean found = false; 

     while ((line=br.readLine())!=null) { 
     String[] s = line.split(" "); 
     list.add(s[0]); 
     list.add(s[1]); 
if (s[0].equals("0") && !found) 
    { 
     found = true; 
     temp.append(line).append("\r\n"); 
    } 
    else if (found) 
    { 
     temp.append(line).append("\r\n"); 
    } 
    } 
br.close(); 
    }  
    catch (Exception e){ 
     e.printStackTrace(); 
    } 

for(int i = 0; i < fieldBoxs.length; i++) { 
    fieldBoxs[i].setSelectedItem(list.get(i)); 
} 

JButton button = new JButton("SAVE"); 
buttonPanel.add(button); 
button.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    { 

    try { 
      StringBuilder con = new StringBuilder(); 
      for (int i = 0; i < fieldBoxs.length; i++) 

        { 
        Object Value = fieldBoxs[i].getSelectedItem(); 
        con.append(" "); 
      con.append(Value); 
      con.append("\r\n"); 
      } 

    FileWriter filewriter = new FileWriter(new File("A.txt")); 
    filewriter.write(con.append(temp).toString()); 
     filewriter.flush(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 
}); 
} 
public static void main(String[] args) { 
A app = new A(); 
JFrame m = new JFrame("A"); 
m.getContentPane().add(app); 
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
m.pack(); 
m.setVisible(true); 
} 
} 

這是被我正在讀的值的文件:測試

2ms 1ms 
1ms 2ms 
0 0 
1 2 
2 1 
1 1 
2 2 

這是怎麼了我的文件現在保存:

2ms 
1ms 
1ms 
2ms 
0 0 
1 2 
2 1 
1 1 
2 2 

我想將其另存爲:

2 1 
1 2 
0 0 
1 2 
2 1 
1 1 
2 2 

格式應該與讀取格式相同,ms也不應出現在新保存的文本文件中。 任何幫助將真正讚賞

回答

0

這裏有一點代碼,將格式化您的示例正確。如果它的工作或長期使用,我不建議使用此代碼,因爲它不靈活或不錯。

String[] cons = con.split("\n"); 
    for(int i = 0; i < cons.length; i++){ 
     if(i == 0 || i == 2){ 
      cons[i] = cons[i] + " "; 
     }else{ 
      cons[i] = cons[i] + "\n"; 
     } 
     } 
    StringBuilder builder = new StringBuilder(); 
    for (String s : cons) { 
     builder.append(s); 
    } 
    con = builder.toString();¨ 

基本上通過不同的線路CON串的代碼迭代(刪除與該拆分lineseperator),並增加了僅當它不排隊一個新的0或2

0
JButton button = new JButton("SAVE"); 
buttonPanel.add(button); 
button.addActionListener(new ActionListener() 
{ 
    public void actionPerformed(ActionEvent e) 
    { 

    try { 

     PrintWriter out = new PrintWriter(new File("A.txt")); 
     out.print(fieldBoxs[0].getSelectedItem()); 
     out.print(" "); 
     out.print(fieldBoxs[1].getSelectedItem()); 
     out.println(); 
     out.print(fieldBoxs[2].getSelectedItem()); 
     out.print(" "); 
     out.print(fieldBoxs[3].getSelectedItem()); 
     out.println(); 
     out.print(temp);   
     out.close();  
    } catch (Exception ex) { 
     ex.printStackTrace(); 
     } 
    } 
});