JavaIO流

来源:互联网 发布:发热巫女知乎 编辑:程序博客网 时间:2024/05/16 10:13
io流java基础基本用法

目录(?)[+]

IO

文件--基本概念

文件是数据源(保存数据的地方)的一种,比如大word文档、jpg文件、MP4文件...都是文件。文件最主要的作用就是保存数据,它既可以保存一张图片,也可以保存视频、声音...等

 

文件流--基本概念

文件在程序中是以流的形式来操作的。


流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径


如何判断是输入流、输出流?

以内存为参照,如果数据流向内存流动,则是输入流;反之,则是输出流。


文件流--分类



java流分为两种流

1、字节流:可以用于读写二进制文件及任何类型文件

2、字符流:可以用于读写文本文件,不能操作二进制文件


实例1.File类的使用

[java] view plaincopyprint?
  1. /** 
  2.  * File类的基本用法 
  3.  */  
  4. package com.io;  
  5.   
  6. import java.io.*;  
  7.   
  8. public class IO1 {  
  9.     public static void main(String[] args) {  
  10.         //创建一个文件对象  
  11.         File f1 = new File("C:\\in.txt");  
  12.           
  13.         //得到文件路径  
  14.         System.out.println("文件路径:" + f1.getAbsolutePath());  
  15.           
  16.         //得到文件的大小,字节数  
  17.         System.out.println("文件大小:" + f1.length());  
  18.           
  19.         //是否可读  
  20.         System.out.println("可读" + f1.canRead());  
  21.           
  22.         //创建文件和创建文件夹  
  23.         File f2 = new File("C:\\in2.txt");  
  24.         //判断文件是否存在  
  25.         if(!f2.exists()){  
  26.             //创建一个文件  
  27.             try {  
  28.                 f2.createNewFile();  
  29.             } catch (IOException e) {  
  30.                 e.printStackTrace();  
  31.             }  
  32.             System.out.println("文件创建成功");  
  33.         } else {  
  34.             System.out.println("文件已存在,无法创建");  
  35.         }  
  36.           
  37.         File f3 = new File("C:\\file1");  
  38.         //判断文件是否为文件夹  
  39.         if(f3.isDirectory()){  
  40.             System.out.println("文件夹已存在");  
  41.         } else {  
  42.             //创建文件夹  
  43.             f3.mkdir();  
  44.             System.out.println("文件夹已创建");  
  45.         }  
  46.           
  47.         //列出一个文件夹下面的所有文件  
  48.         File f4 = new File("C:\\");  
  49.           
  50.         if(f4.isDirectory()){  
  51.             //获取文件数组  
  52.             File[] lists = f4.listFiles();  
  53.             for(int i = 0; i < lists.length; i++){  
  54.                 System.out.println("文件名:" + lists[i].getName());  
  55.             }  
  56.               
  57.         }  
  58.           
  59.           
  60.     }  
  61.   
  62. }  


实例2.文件字节流的使用

[java] view plaincopyprint?
  1. /** 
  2.  * FileInputStream类的使用 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO2 {  
  7.   
  8.       
  9.     public static void main(String[] args) {  
  10.         //创建一个文件对象  
  11.         File f = new File("C:\\in.txt");  
  12.         FileInputStream fis = null;  
  13.         //File无读写能力,所以需要使用InputStream进行读入  
  14.         try {  
  15.             fis = new FileInputStream(f);  
  16.               
  17.             //定义一个字节数组,相当于缓存  
  18.             byte[] bytes = new byte[1024];  
  19.             //得到实际读取到的字节数  
  20.             int n = 0;  
  21.             //循环读取  
  22.             while((n = fis.read(bytes)) != -1){  
  23.                 //把字节转换成String  
  24.                 String s = new String(bytes, 0, n);  
  25.                 System.out.println(s);  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally {  
  31.             //关闭文件流--必须放这里  
  32.             try {  
  33.                 fis.close();  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.     }  
  39.   
  40. }  


[java] view plaincopyprint?
  1. /** 
  2.  * FileOutputStream类的使用 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO3 {  
  7.     public static void main(String[] args) {  
  8.         //创建文件对象  
  9.         File f = new File("C:\\out2.txt");  
  10.           
  11.         //字节输出流  
  12.         FileOutputStream fos = null;  
  13.           
  14.         try {  
  15.             fos = new FileOutputStream(f);  
  16.             String s = "hello,world\r\n";  
  17.             String s2 = "hello,java\r\n";  
  18.             //定义字节数组  
  19.             //byte[] bytes = new byte[1024];  
  20.               
  21.             fos.write(s.getBytes());  
  22.             fos.write(s2.getBytes());  
  23.         } catch (Exception e) {  
  24.             e.printStackTrace();  
  25.         } finally {  
  26.             try {  
  27.                 fos.close();  
  28.             } catch (IOException e) {  
  29.                 e.printStackTrace();  
  30.             }  
  31.         }  
  32.   
  33.     }  
  34.   
  35. }  

[java] view plaincopyprint?
  1. /** 
  2.  * 图片拷贝 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO4 {  
  7.     public static void main(String[] args) {  
  8.         //先把图片读入到内存 -> 写入到某个文件  
  9.         //因为是二进制文件,因此只能用字节流完成  
  10.           
  11.         //输入流  
  12.         FileInputStream fis = null;  
  13.         //输出流  
  14.         FileOutputStream fos = null;  
  15.          try {  
  16.             fis = new FileInputStream(new File("C:\\image01.jpg"));  
  17.             fos = new FileOutputStream(new File("C:\\image01_copy.jpg"));  
  18.               
  19.             byte[] buf = new byte[1024];  
  20.             //记录实际读取到的字节  
  21.             int n = 0;  
  22.             //循环读取  
  23.             while((n = fis.read(buf)) != -1){  
  24.                 //输出到指定文件  
  25.                 fos.write(buf);  
  26.             }  
  27.         } catch (Exception e) {  
  28.             e.printStackTrace();  
  29.         } finally {  
  30.             //关闭打开的文件流  
  31.             try {  
  32.                 fis.close();  
  33.                 fos.close();  
  34.             } catch (IOException e) {  
  35.                 e.printStackTrace();  
  36.             }  
  37.         }  
  38.   
  39.     }  
  40.   
  41. }  


实例3.文件字节流

[java] view plaincopyprint?
  1. /** 
  2.  * 字符流 
  3.  */  
  4. package com.io;  
  5. import java.io.*;  
  6. public class IO5 {  
  7.     public static void main(String[] args) {  
  8.         //文件读入字符流  
  9.         FileReader fr = null;  
  10.         //文件写出字符流  
  11.         FileWriter fw = null;  
  12.           
  13.         try {  
  14.             //创建文件读入字符流对象  
  15.             fr = new FileReader(new File("C:\\test.txt"));  
  16.             //创建文件写出字符流对象  
  17.             fw = new FileWriter(new File("C:\\test_copy.txt"));  
  18.               
  19.             //读入到内存  
  20.             //缓存char数组  
  21.             char[] c = new char[1024];  
  22.             //读入实际大小  
  23.             int n = 0;  
  24.             while((n = fr.read(c)) != -1){  
  25.                 fw.write(c, 0, n);  
  26.             }  
  27.               
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         } finally {  
  31.             //关闭文件流  
  32.             try {  
  33.                 fr.close();  
  34.                 fw.close();  
  35.             } catch (IOException e) {  
  36.                 e.printStackTrace();  
  37.             }  
  38.         }  
  39.     }  
  40.   
  41. }  


实例4.缓存字节流

[java] view plaincopyprint?
  1. /** 
  2.  * 缓冲字符流 
  3.  *  
  4.  */  
  5. package com.io;  
  6. import java.io.*;  
  7. public class IO6 {  
  8.   
  9.       
  10.     public static void main(String[] args) {  
  11.         //缓冲字符流定义  
  12.         BufferedReader br = null;  
  13.         BufferedWriter bw = null;  
  14.           
  15.         try {  
  16.             //创建FileReader对象  
  17.             FileReader fr = new FileReader(new File("C:\\test.txt"));  
  18.             //创建FileWriter对象  
  19.             FileWriter fw = new FileWriter(new File("C:\\test_copy2.txt"));  
  20.               
  21.             //创建缓冲字符流  
  22.             br = new BufferedReader(fr);  
  23.             bw = new BufferedWriter(fw);  
  24.               
  25.               
  26.             //循环读文件  
  27.             //临时字符串  
  28.             String s = "";  
  29.             while((s = br.readLine()) != null){  
  30.                 //输出到文件  
  31.                 bw.write(s + "\r\n");  
  32.             }  
  33.         } catch (Exception e) {  
  34.             e.printStackTrace();  
  35.         } finally {  
  36.               
  37.             //关闭缓冲字符流  
  38.             try {  
  39.                 br.close();  
  40.                 bw.close();  
  41.             } catch (IOException e) {  
  42.                 e.printStackTrace();  
  43.             }  
  44.         }  
  45.           
  46.           
  47.   
  48.     }  
  49.   
  50. }  


实例5.记事本

[java] view plaincopyprint?
  1. /** 
  2.  * 记事本(界面+功能) 
  3.  */  
  4. package com.notepad;  
  5.   
  6. import java.io.*;  
  7. import java.awt.*;  
  8. import java.awt.event.*;  
  9.   
  10. import javax.swing.*;  
  11.   
  12. public class NotePad extends JFrame implements ActionListener{  
  13.     //定义组件  
  14.     //文本域  
  15.     JTextArea jta = null;  
  16.     //滚动条  
  17.     JScrollPane jsp = null;  
  18.     //菜单条  
  19.     JMenuBar jmb =null;  
  20.     //菜单栏目  
  21.     JMenu jm = null;  
  22.     //菜单项  
  23.     JMenuItem jmi1 = null;  
  24.     JMenuItem jmi2 = null;  
  25.       
  26.     //构造方法  
  27.     public NotePad(){  
  28.         //创建组件  
  29.         jta = new JTextArea();  
  30.         jsp = new JScrollPane(jta);  
  31.         jmb = new JMenuBar();  
  32.         jm = new JMenu("文件(F)");  
  33.         jmi1 = new JMenuItem("打开(O)");  
  34.         jmi2 = new JMenuItem("保存(S)");  
  35.           
  36.         //设置助记符  
  37.         jm.setMnemonic('F');  
  38.         jmi1.setMnemonic('O');  
  39.         jmi2.setMnemonic('S');  
  40.           
  41.         //设置监听器  
  42.         jmi1.addActionListener(this);  
  43.         jmi2.addActionListener(this);  
  44.           
  45.         //设置动作监听器反应命令  
  46.         jmi1.setActionCommand("open");  
  47.         jmi2.setActionCommand("save");  
  48.           
  49.         //设置菜单条  
  50.         setJMenuBar(jmb);  
  51.           
  52.         //把菜单栏目放入菜单条  
  53.         jmb.add(jm);  
  54.           
  55.         //菜单项放入菜单栏  
  56.         jm.add(jmi1);  
  57.         jm.add(jmi2);  
  58.           
  59.           
  60.         //加入到JFrame  
  61.         add(jsp);  
  62.           
  63.           
  64.         //设置窗体  
  65.         setTitle("我的记事本");  
  66.         setSize(400,300);  
  67.         setLocationRelativeTo(null);  
  68.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  69.         setVisible(true);  
  70.     }  
  71.       
  72.       
  73.     public static void main(String[] args) {  
  74.         NotePad np = new NotePad();  
  75.   
  76.     }  
  77.   
  78.     //动作监听器实现  
  79.     @Override  
  80.     public void actionPerformed(ActionEvent e) {  
  81.           
  82.         if(e.getActionCommand().equals("open")){  
  83.             //文件选择框  
  84.             JFileChooser jfc = new JFileChooser();  
  85.             jfc.setDialogTitle("打开文件");  
  86.             jfc.showOpenDialog(null);  
  87.             jfc.setVisible(true);  
  88.             String file = jfc.getSelectedFile().getAbsolutePath();  
  89.             //设置缓冲读入流  
  90.             BufferedReader br = null;  
  91.             try {  
  92.                 br = new BufferedReader(new FileReader(new File(file)));  
  93.                 //临时字符串  
  94.                 String s = "";  
  95.                 String all = "";  
  96.                 while((s = br.readLine()) != null){  
  97.                     //因为readLine方法会去掉回车换行  
  98.                     all += s + "\r\n";  
  99.                 }  
  100.                 jta.setText(all);  
  101.             } catch (Exception e2) {  
  102.                 e2.printStackTrace();  
  103.             } finally {  
  104.                 try {  
  105.                     br.close();  
  106.                 } catch (IOException e1) {  
  107.                     e1.printStackTrace();  
  108.                 }  
  109.             }  
  110.               
  111.         } else if(e.getActionCommand().equals("save")){  
  112.             //文件选择框  
  113.             JFileChooser jfc = new JFileChooser();  
  114.             jfc.setDialogTitle("保存文件");  
  115.             jfc.showSaveDialog(null);  
  116.             jfc.setVisible(true);  
  117.               
  118.             String file = jfc.getSelectedFile().getAbsolutePath();  
  119.             //设置缓冲写出流  
  120.             BufferedWriter bw = null;  
  121.             try {  
  122.                 bw = new BufferedWriter(new FileWriter(new File(file)));  
  123.                 //临时存放JTextArea中的字符串  
  124.                 String s = jta.getText();  
  125.                 //将字符串按一行分割成字符串数组  
  126.                 String[] ss = s.split("\r\n");  
  127.                 //循环写入写出流  
  128.                 for(int i = 0; i < ss.length; i++){  
  129.                     bw.write(ss[i] + "\r\n");  
  130.                 }  
  131.             } catch (Exception e2) {  
  132.                 e2.printStackTrace();  
  133.             } finally {  
  134.                 try {  
  135.                     bw.close();  
  136.                 } catch (IOException e1) {  
  137.                     e1.printStackTrace();  
  138.                 }  
  139.             }  
  140.               
  141.         } else {  
  142.             System.out.println("无效动作");  
  143.         }  
  144.           
  145.     }  
  146.   
  147. }  
0 0
原创粉丝点击