IO流中File文件最常用和直接的用法

一、File

(一)概述
1、File:
1)文件和目录(文件夹)路径名的抽象表示形式
2)注意:
  • 可以表示文件夹与文件,即路径与文件
  • 抽象概念,即路径与文件是否存在不确定
 
2、构造方法
1)File(String pathname):根据一个路径得到File对象
  • File file = new File("E:\demo\a.txt");
2)File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
  • File file2 = new File("E:\demo", "a.txt");
3)File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
  •   File file3 = new File("e:\demo");
  •   File file4 = new File(file3, "a.txt");
  1 package ltb20180106;
  2 
  3 import javax.swing.*;
  4 import java.awt.*;  
  5 import java.awt.event.*;
  6 import java.io.*;
  7     
  8     
  9 class ChineseTextEdit {
 10     
 11         private JFrame jf;
 12         private Chinese jp2;
 13         private JTextArea jt;
 14         private JButton save;
 15         private JButton exit;
 16         private JButton cancel;
 17         private JScrollPane js;
 18         private File f;
 19         private FileWriter fw;
 20         
 21     public ChineseTextEdit() {
 22         
 23         
 24         try {
 25             
 26             jp2=new Chinese();
 27             f=new File("D:\myRead\ltb6w.txt");
 28             fw=new FileWriter(f);
 29             save=new JButton("保存");
 30             save.addActionListener(jp2);
 31             exit=new JButton("退出");
 32             exit.addActionListener(jp2);
 33             cancel=new JButton("取消");
 34             cancel.addActionListener(jp2);
 35             
 36             
 37             jp2.setLayout(new FlowLayout());
 38             jp2.add(save);
 39             jp2.add(cancel);
 40             jp2.add(exit);
 41         
 42             jt=new JTextArea();
 43             jt.setRows(6);
 44             jt.setLineWrap(true);//自动换行
 45             
 46             js=new JScrollPane(jt);
 47             js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);//水平滚动条
 48             js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);//垂直滚动
 49             
 50             
 51             jf=new JFrame("文本编辑器");
 52             jf.setSize(400, 200);
 53             jf.setLayout(new BorderLayout());
 54              
 55             
 56             jf.add(js,BorderLayout.NORTH);
 57             jf.add(jp2,BorderLayout.SOUTH);
 58         
 59             
 60             jf.setLocationRelativeTo(null);
 61             jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
 62             jf.setVisible(true);
 63             
 64             
 65         }catch(Exception e) {
 66             
 67             System.out.println(e.getMessage());
 68         }
 69         
 70         
 71     }
 72     
 73     
 74     @SuppressWarnings("serial")
 75     class Chinese extends JPanel implements ActionListener {
 76  
 77         private String bname;
 78         private String text;
 79         
 80         Chinese() {
 81             
 82             //System.out.println("Chinese");
 83         }
 84          
 85         @Override
 86         public void actionPerformed(ActionEvent arg0) {
 87             
 88             bname=arg0.getActionCommand();
 89             
 90             if(bname.equals("保存")) {
 91                 
 92                 text=jt.getText();
 93                 
 94                 try {
 95                     fw.write(text);
 96                     fw.flush();
 97                     fw.close();
 98                 }catch (IOException e) {
 99                     
100                     System.out.println(e.getMessage());
101                 }
102                 
103                 
104             }else if (bname.equals("取消")) {
105                 
106                 jt.setText("");
107                 
108             }else if(bname.equals("退出")) {
109                 
110                 jf.dispose();
111             }
112             
113             
114         }
115         
116     }
117 
118     public static void main(String[] args) {
119         
120         new ChineseTextEdit();
121 
122     }
123 
124 }

 IO流中File文件最常用和直接的用法