请问如何使用BufferedWriter 和 FileWriter类实现,将JList列表框中的所有信息存于一个指定目录的文件中,格式不变,我对着还不熟悉, 请高手指教
------解决方案--------------------
ListModel listModel = jList.getModel();
BufferedWriter bw = new BufferedWriter( new FileWriter( fileName ) );
for( int i = 0, n = listModel.getSize(); i < n; i++ ) {
String s = listModel.getElementAt( i ).toString();
bw.write( s, 0, s.length() );
bw.newLine();
}
bw.close();
------解决方案--------------------
JList jl = new JList();//已知JList;
String fileName = "";
try{
FileWriter fw = new FileWriterI(new File(fileName));
BufferedWriter bw = new BufferedWriter(fw);
Object [] objs = jl.getSelectedValues();
for(int i=0;i<objs.length;i++){
String line = objs[i].toString()+"\r\n";
bw.write(line,0,line.length());
}
bw.close();
}catch(Exception e){}