Java笔记——IO流

来源:互联网 发布:macbook怎么下不了淘宝 编辑:程序博客网 时间:2024/06/17 01:41

一、文件的读写

java中文件的读写有多种方式:

1、按字节读写文件内容,常用于读取二进制文件,如图片、声音、影音等文件。
例如:使用字节流拷贝图片1.bmp到2.bmp
import java.io.*;class CopyPic {    public static void main(String[] args) {        FileInputStream fis = null;        FileOutputStream fos = null;        try {            fis = new FileInputStream("1.bmp");            fos = new FileOutputStream("2.bmp");            byte[] buf = new byte[1024];            int len = 0;            while((len = fis.read(buf)) != -1)                fos.write(buf, 0, len);        }        catch (IOException e) {            throw new RuntimeException("复制文件失败");        }        finally {            try {                if(fis != null)                    fis.close();            }            catch (IOException e) {                throw new RuntimeException("读取关闭失败");            try {                if(fos != null)                    fos.close();            }            catch (IOException e) {                throw new RuntimeException("写入关闭失败");            }        }    }}      

2、以字符读取文件内容,常用于读写文本,数字等类型的文件。
例如:使用字符流拷贝1.txt文本到2.txt
import java.io.*;class CopyTxt {    public static void main(String[] args) {        BufferedReader bufr = null;        BufferedWriter bufw = null;        try {            bufr = new BufferedReader(new FileReader("1.txt"));            bufw = new BufferedWriter(new FileWriter("2.txt"));            String line = null;            while((line = bufr.readLine()) != null) { //readLine函数不提供换行符                bufw.write(line);                bufw.newLine(); //相当于添加换行符                bufw.flush();            }        }        catch (IOException e) {            throw new RuntimeException("读写失败");        }        finally {            try {                if(bufr != null)                    bufr.close();            }            catch (IOException e) {                throw new RuntimeException("读取关闭失败");            try {                if(bufw != null)                    bufw.close();            }            catch (IOException e) {                throw new RuntimeException("写入关闭失败");            }        }    }}    

二、从控制台读取字符串
例如:使用BufferedReader类来获取
package com.test;    import java.io.BufferedReader;  import java.io.IOException;  import java.io.InputStreamReader;    public class MainRun {      public static void main(String[] args) {          try {              BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));              System.out.print("请输入一个字符串:");              String str = strin.readLine();              System.out.println("第一个:"+str);              System.out.println("请输入第二个字符串:");              String str2 = strin.readLine();              System.out.println("第2个:"+str2);          } catch (IOException e) {              e.printStackTrace();          }      }  }  

例如:使用Scanner类来获取
package com.test;    import java.util.Scanner;    public class MainRun {      public static void main(String[] args) {          Scanner sc = new Scanner(System.in);           System.out.println("输入第一个boolean值(true/false):");          if(sc.nextBoolean()){              System.out.println("输入布尔:真的");          }else{              System.out.println("输入布尔:假的");          }          System.out.println("输入第一个数字:");          System.out.println("输入数字:"+sc.nextInt());            System.out.println("输入一个字符串:");          System.out.println("输入字符串:"+sc.next());            System.out.println("输入一个长整型:");          System.out.println("输入长整型:"+sc.nextLong());      }  }  

0 0
原创粉丝点击