java文件读写

    xiaoxiao2021-04-11  37

    java文件读写涉及到很多IO操作,读写方法较多,下面从字节流、字符流、节点流、处理流等方面进行介绍。

    1.基本概念

    输出流:程序->文件 输入流:文件->程序 字节流:一次读入或者读出是8位二进制字符流:一次读入或者读出是16位二进制流节点流:直接与数据源相连。处理流:在节点流上再套接一层,对一个已知流的连接和封装,构造方法带一个其他流对象作参数,通过所封装的流的功能调用实现数据读写。一个流对象经过其他流的多次包装,称为流的连接。

    2.基本关系

    红色标记的为与文件读写有关的类,带Buffered前缀的是处理流,会设置一个缓冲区,减少读写磁盘的次数。左边代表字符流,右边代表字节流。

    3.例子

    import java.io.*; public class Exam1 { public static void main(String[] args) throws IOException { String filename ="file"+File.separator+"test1.txt"; String outputfile ="file"+File.separator+"output.txt"; fileInpputStream(filename); fileOutputStream(); bufferedInputStream(filename); bufferedOutputStream(filename,outputfile); readFileByChars(filename); writeByFileReader(outputfile); readByBufferedReader(filename); writeByBufferedReader(outputfile); } /** * 字节流:FileInputStream FileOutputStream */ public static void fileInpputStream(String fileName) { // 一般先创建file对象 FileInputStream fileInput = null; try { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } byte[] buffer = new byte[1024]; fileInput = new FileInputStream(file); int byteread = 0; // byteread表示一次读取到buffers中的数量。 while ((byteread = fileInput.read(buffer)) != -1) { System.out.write(buffer, 0, byteread); } } catch (Exception e) { // TODO: handle exception } finally { try { if (fileInput != null) { fileInput.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void fileOutputStream() { FileOutputStream fop = null; File file; String content = "fileOutputStream"; try { file = new File("file"+File.separator+"test1.txt"); fop = new FileOutputStream(file,true); if (!file.exists()) { file.createNewFile(); } byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fop != null) { fop.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 缓冲字节流:BufferedInputStream * @throws IOException */ public static void bufferedInputStream(String filename)throws IOException { File file = new File(filename); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); byte[] contents = new byte[1024]; int byteRead = 0; String strFileContents; try { while((byteRead = bis.read(contents)) != -1){ strFileContents = new String(contents,0,byteRead); System.out.println(strFileContents); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } bis.close(); } /** * 缓冲字节流:BufferedOutputStream * @throws IOException */ public static void bufferedOutputStream(String filename,String outputfile) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputfile)); int i; do { i = bis.read(); if (i != -1) { bos.write(i); } } while (i != -1); bis.close(); bos.close(); } /** * 字符流:FileReader */ public static void readFileByChars(String fileName) { FileReader reader = null; System.out.print("FileReader:"); try { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } reader = new FileReader(file); char[] buffer = new char[1024]; int charread = 0; while ((charread = reader.read(buffer)) != -1) { System.out.print(buffer); } } catch (IOException e) { // TODO: handle exception } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 字符流:FileWriter */ public static void writeByFileReader(String output) { try { String data = " This content will append to the end of the file"; File file = new File(output); if (!file.exists()) { file.createNewFile(); } FileWriter fileWritter = new FileWriter(file.getName(), true); fileWritter.write(data); fileWritter.close(); } catch (IOException e) { e.printStackTrace(); } } /** 缓冲字符流 BufferedReader BufferedWriter * * @param fileName */ public static void readByBufferedReader(String fileName) { System.out.println("BufferedReader:"); try { File file = new File(fileName); // 读取文件,并且以utf-8的形式写出去 BufferedReader bufread; String read; bufread = new BufferedReader(new FileReader(file)); while ((read = bufread.readLine()) != null) { System.out.println(read); } bufread.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } public static void writeByBufferedReader(String output) { try { String content = "This is the content to write into file"; File file = new File(output); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.flush(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-666652.html

    最新回复(0)