Java中的文件和stream流的操作代码

1.Java中FileRead方法的运用代码及详解

package example2;
import java.io.FileReader;
import java.io.IOException;
class FileRead{
    public static void main(String[] args) throws IOException{
        //创建一个FileWriter对象
    //FileWriter fw=new FileWriter("File01.txt",true);
    FileReader fr=new FileReader("File01.txt");
    //如果写入的文件已经存在,则会覆盖写入
    //如果要追加写入,则要
    //调用write方法对文件写入数据
    //在windows中,换行符为\r\n
    //在macos中,前期换行符为\r,现在是\n
    //在linux操作系统中,换行符为\n
    /*    int ch;
        while((ch=fr.read())!=-1) {
            System.out.println((char) ch);
        }
    fr.close();*/
    //使用字符数组进行批量读取数据
    char [] buf=new char[3];
    int len;
    while((len=fr.read(buf))!=-1) {
        String str=new String(buf,0,len);
        System.out.println(str);
    }
    /*
     * FileWriter当中的5中写入数据的方法
     * fw.write(int a);参照ASCII表和UNICODE表
     * fw.write(String str)写入完整的字符串
     * fw.write(String str,int offset,int count)写入字符串的一部分
     * fw.write(char[] array)写入完整的字符数组
     * fw.write(char[] array,int offset,int count)写入字符数组的一部分
     *
     * */
    }
}

//Java中的FileWrite方法的使用及详解

package example2;
import java.io.FileWriter;
import java.io.IOException;
class FileWrite{
    public static void main(String[] args) throws IOException {
        //创建一个FileWriter对象
    //FileWriter fw=new FileWriter("File01.txt",true);
    FileWriter fw=new FileWriter("File01.txt");
    //如果写入的文件已经存在,则会覆盖写入
    //如果要追加写入,则要
    //调用write方法对文件写入数据
    //在windows中,换行符为\r\n
    //在macos中,前期换行符为\r,现在是\n
    //在linux操作系统中,换行符为\n
    /*
     * FileWriter当中的5中写入数据的方法
     * fw.write(int a);参照ASCII表和UNICODE表
     * fw.write(String str)写入完整的字符串
     * fw.write(String str,int offset,int count)写入字符串的一部分
     * fw.write(char[] array)写入完整的字符数组
     * fw.write(char[] array,int offset,int count)写入字符数组的一部分
     *
     * */
    fw.write("......daniahhsh");
    fw.write("zhaoliying\r\n");
    String str="sgsgjajhgs";
    fw.write(97);
    fw.write(str,0,5);
    char[] array= {'v','k','n','v','g'};
    fw.write(array,1,3);
    fw.write("......bajabjd");
    //调用close方法关闭文件
    fw.close();
    }
}

//Java中BufferRead的使用方法及详解

package example2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferRead{
    //BufferedReader额外的为大家提供了一个readLine()函数进行读取以整行的字符串。
    //BufferWriter中额外的提供了一个换行函数newLine();
    // BufferWriter与filewriter类似,BufferWriter内部有一个长度为8192的char[]字符数组缓冲区。
    // 每次写数据时,是像缓冲区中添加字符。如果缓冲数组满了,然后统一写到硬盘的文件中。
    // 如果最终关闭时,数组仍然没满,那么就将剩余的有效部分写到硬盘文件里。
    public static void main(String[] args) throws IOException {
        //首先创建一个普通的FileWriter
        FileReader fw = new FileReader("File02.txt");
        //将这个普通的FileWriter对象传递给BufferedWriter构造方法即可。
        BufferedReader bw=new BufferedReader(fw);
        //读取单个字符串
        /*int ch;
        while((ch=bw.read())!=-1) {
            System.out.println((char) ch);
        }*/
        
        //读取字符数组(一次性从缓冲区里读取多个字符)
        /*int len;
        char[] buf=new char[3];
        while((len=bw.read(buf))!=-1) {
            String str=new String(buf,0,len);
            System.out.println(str);
        }*/
        //读取以整行的字符串
        String line;
        while((line=bw.readLine())!=null) {
            System.out.println(line);
        }
        bw.close();
    }
}

Java中BufferWrite方法的运用及详解

package example2;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferWrite {
    //BufferWriter中额外的提供了一个换行函数newLine();
    // BufferWriter与filewriter类似,BufferWriter内部有一个长度为8192的char[]字符数组缓冲区。
    // 每次写数据时,是像缓冲区中添加字符。如果缓冲数组满了,然后统一写到硬盘的文件中。
    // 如果最终关闭时,数组仍然没满,那么就将剩余的有效部分写到硬盘文件里。
    public static void main(String[] args) throws IOException {
        //首先创建一个普通的FileWriter
        FileWriter fw = new FileWriter("File02.txt");
        //将这个普通的FileWriter对象传递给BufferedWriter构造方法即可。
        BufferedWriter bw=new BufferedWriter(fw);
        bw.write("sdfghjkl");
        bw.newLine();//这个函数会根据你使用的操作系统的不同,而修改换行符的使用。很灵活,在开发中提倡常用。
        bw.write("etrytuyxcv");
        bw.close();
    }
}

//stream流的使用及详解

package example3;

import java.util.ArrayList;
import java.util.stream.Stream;

public class listStream {
    // java8中的流其实是一个stream的接口对象
    // jdk中提供了一个java.util.stream.Stream<T>;
    public static void main(String[] args) {
        // 1.通过一个集合获取流
        ArrayList<String> list = new ArrayList<>();
        list.add("赵丽颖,15");
        list.add("鹿晗,18");
        list.add("李易峰,20");
        list.add("苏宝娟,10");
        Stream<String> stream = list.stream();

        // 根据数组获取一个流
        String[] arrayStr = new String[] { "dfgha", "tyui", "erqq" };
        Stream<String> stream1 = Stream.of(arrayStr);

        Integer[] arrayInt = new Integer[] { 10, 20, 50, 40, 60 };
        Stream<Integer> stream3 = Stream.of(arrayInt);
        list.stream().map(s -> s.split(",")[1]).map(Integer::parseInt).filter(n -> n >= 15)
                .forEach(System.out::println);

        // 获取流之后,可以使用映射方法:map(用于转换的lambda表达式)
        Stream<Integer> arrayIntger = list.stream().map((String str) -> {
            int num = Integer.parseInt(str);
            return num;
        });

        // lambda表达式
        // s->System.out.println(s);
        // 对象名调用成员方法
        // System.out::println();
        // 如果流当中的元素特别多,那么一个人挨个处理比较慢,效率低。
        // 对流当中的元素使用多个人处理。称为“并发”
        // 如何获取并发流(.parallelStream())
        System.out.println("================");
        //如此调用也是同样的效果
        //使用并发流会有多个人同时抢占位置,顺序会被打乱,但不会出现重复。
        list.stream().parallel().forEach(System.out::println);
        
        System.out.println("================");
        
        list.parallelStream().forEach(System.out::println);
        // 注:使用并发流处理时,到底有几个人同时操作不用管,jdk会处理。
        // 只要正确使用,就不会出现多个人同时抢到一个元素。
    }
}

对于Java中的一些很实用方法进行了一下归类。都是本人实际操作运行成功的案例代码。可以避免少走一些弯路。

革命尚未成功,同志仍需努力!

猜你喜欢

转载自www.cnblogs.com/aasu/p/9119766.html