String read=null; try{ out=new PrintWriter(new BufferedWriter(new FileWriter("f:\\temp\\123xx.txt"))); while ((read = in.readLine()) != null) { if (read.equals("exit")) break; out.println(read); } }catch (Exception ex){ ex.printStackTrace(); }finally{ if (out!=null){ out.close(); } }
~~~from java doc以下代码弹出一个针对用户主目录的文件选择器,其中只显示 .jpg 和 .gif 图像: JFileChooser chooser = new JFileChooser(); // Note: source for ExampleFileFilter can be found in FileChooserDemo, // under the demo/jfc directory in the JDK. ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("jpg"); filter.addExtension("gif"); filter.setDescription("JPG & GIF Images"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(parent); if(returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); }
PrintWriter out=null;
String read=null;
try{
out=new PrintWriter(new BufferedWriter(new FileWriter("f:\\temp\\123xx.txt")));
while ((read = in.readLine()) != null) {
if (read.equals("exit"))
break;
out.println(read);
}
}catch (Exception ex){
ex.printStackTrace();
}finally{
if (out!=null){
out.close();
}
}
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
// under the demo/jfc directory in the JDK.
ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}