zoukankan      html  css  js  c++  java
  • 文件总结

    输入流的代码;

    代码部分:

    package lianxi;
    import java.io.*;
    public class bo
    {
    public static void main(String[] args) throws IOException {
    //创建字节输入流
    FileInputStream fis = new FileInputStream("D://a.txt");
    //创建一个长度为1024竹筒
    byte[] bbuf = new byte[1024];
    //用于保存实际读取的字节数
    int hasRead = 0;
    //使用循环来重复取水过程
    while ((hasRead = fis.read(bbuf)) > 0) {
    //取出竹筒中水滴(字节),将字节数组转换成字符串输入!
    System.out.print(new String(bbuf, 0, hasRead));
    }
    fis.close();
    }
    }

     

     

    运行结果分析,左后也是成功的吧a.txt文件里的文字输出出来了;

    代码部分:

    package lianxi;
    import java.io.*;
    public class bo
    {
    public static void main(String[] args) throws IOException {
    FileReader fr = null;
    try
    {
    //创建字符输入流
    fr = new FileReader("D://b.txt");
    //创建一个长度为32竹筒
    char[] cbuf = new char[32];
    //用于保存实际读取的字符数
    int hasRead = 0;
    //使用循环来重复取水过程
    while ((hasRead = fr.read(cbuf)) > 0 )
    {
    //取出竹筒中水滴(字节),将字符数组转换成字符串输入!
    System.out.print(new String(cbuf , 0 , hasRead));
    }
    }
    catch (IOException ioe)
    {
    ioe.printStackTrace();
    }
    finally
    {
    //使用finally块来关闭文件输入流
    if (fr != null)
    {
    fr.close();
    }
    }
    }
    }

     运行结果:

     

     

     文件部分:

     

     

     运行结果分析:

    最后同样吧指定路径下的文件里的内荣输出出来了。

    一天的收获:

    知道了如何运用输入流的代码。

    输出流的代码:

    代码部分:

    package lianxi;
    import java.io.*;
    public class bo
    {
    public static void main(String[] args) throws IOException {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
    //创建字节输入流
    fis = new FileInputStream("D://b.txt");
    //创建字节输入流
    fos = new FileOutputStream("D://c.txt");
    byte[] bbuf = new byte[32];
    int hasRead = 0;
    //循环从输入流中取出数据
    while ((hasRead = fis.read(bbuf)) > 0) {
    //每读取一次,即写入文件输出流,读了多少,就写多少。
    fos.write(bbuf, 0, hasRead);
    }
    } catch (IOException ioe) {
    ioe.printStackTrace();
    } finally {
    //使用finally块来关闭文件输入流
    if (fis != null) {
    fis.close();
    }
    //使用finally块来关闭文件输出流
    if (fos != null) {
    fos.close();
    }
    }
    }
    }

     运行结果:

     

     

    运行结果分析:

    成功的吧b文件里的内容输入道了c文件里

    代码部分:

    package lianxi;
    import java.io.*;
    public class bo
    {
    public static void main(String[] args) throws IOException {
    FileWriter fw = null;
    try {
    //创建字符输出流
    fw = new FileWriter("D://c.txt");
    fw.write("锦瑟 - 李商隐 ");
    fw.write("锦瑟无端五十弦,一弦一柱思华年。 ");
    fw.write("庄生晓梦迷蝴蝶,望帝春心托杜鹃。 ");
    fw.write("沧海月明珠有泪,蓝田日暖玉生烟。 ");
    fw.write("此情可待成追忆,只是当时已惘然。 ");
    } catch (IOException ioe) {
    ioe.printStackTrace();
    } finally {
    //使用finally块来关闭文件输出流
    if (fw != null) {
    fw.close();
    }
    }
    }
    }

     运行结果:

    运行结果分析:

    成功的吧李商隐的锦瑟输入到c文件 里面;

    输出流复制文件:

    代码部分:

    package lianxi;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardOpenOption;

    public class bo
    {
    public void copyFiles(Path originPath, Path destinationPath)
    throws IOException {
    if (Files.notExists(originPath) 
    || Files.exists(destinationPath)) {
    throw new IOException(
    "Origin file must exist and " + 
    "Destination file must not exist");
    }
    byte[] readData = new byte[1024];
    try (InputStream inputStream = Files.newInputStream(originPath, 
    StandardOpenOption.READ);
    OutputStream outputStream = Files.newOutputStream(destinationPath, 
    StandardOpenOption.CREATE)) {
    int i = inputStream.read(readData);
    while (i != -1) {
    outputStream.write(readData, 0, i);
    i = inputStream.read(readData);
    }
    } catch (IOException e) {
    throw e;
    }
    }

    public static void main(String[] args) {
    bo test = new bo();
    Path origin = Paths.get("D:/c.txt");
    Path destination = Paths.get("D:/d.txt");
    try {
    test.copyFiles(origin, destination);
    System.out.println("Copied Successfully");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

     运行结果:

     

     

    运行结果分析:

    最后也是成功的吧C文件里的内荣复制到D文件里面了。

     

     

  • 相关阅读:
    [POJ 1269]Intersecting Lines
    [POJ 3304]Segments
    [HNOI 2011]数学作业
    [UOJ 12]猜数
    [UOJ 282]长度测量鸡
    [HAOI 2007]理想的正方形
    [POJ 2318]TOYS
    [SDOI 2009]HH的项链
    [USACO 12DEC]Running Away From the Barn
    [HDU 2036]改革春风吹满地
  • 原文地址:https://www.cnblogs.com/092e/p/14176836.html
Copyright © 2011-2022 走看看