笔记-迎难而上之Java基础进阶6

import java.io.*;
public class InputStreamDemo{
    public static void main(String[] args) throws IOException{
        //创建输入流对象,创建对象时绑定读取的数据源
        FileInputStream fis = new FileInputStream("E:\\study\\demo12\\c.txt");
        //创建byte[] 数组,让他一次性读取多个字节
        byte[] bytes = new byte[1024];
        int len=0;
        //用len来接收读取到的有效字节
        //因为文件中有5个字节,所以把五个字节传递给len
        while((len=fis.read(bytes))!=-1){
            //String的构造方法,可以把字节数组变成字符串
            System.out.println(new String(bytes));
        }
        fis.close();
    }
}

文件复制

package demo16;

import java.io.*;
public class CopyDemo{
    public static void main(String[] args) throws IOException{
        //创建FileInputStream对象,需要指定读取的数据源文件的位置
        FileInputStream fis = new FileInputStream("C:\\CloudMusic\\MV\\周二珂 - 告白气球.mp3");
        //绑定要输出的的数据目的地
        FileOutputStream fos = new FileOutputStream("E:\\study\\demo13\\周二珂 - 告白气球.mp3");
        //创建字节数组,让他一次性读取多个文件
        byte[] bytes  = new byte[1024];
        //创建一个整数变量,让他接收读取到的有效字节和写入的有效字节
        int date = 0;
        //当读取到文件末尾时会返回-1,只要读取的字节不等于-1,就继续读取
        //把读取到的有效字节,赋值给date,只要不等于-1,就写入文件
        //从输入流中读取一些字节数并将它们存储到缓冲区数组bytes中。
        while((date=fis.read(bytes))!=-1){
            //读一个字节写一个字节
            //为什么写入的数据是bytes??,因为读取的数据就存储到bytes中
            fos.write(bytes,0,date);
        }
        fos.close();
        fis.close();
    }
}

字符输入流Reader类

Reader类是字符流最顶层的类

成员方法:

  1. int read() 读取单个字符并返回
  2. int read(char[] cubf)一次性读取多个字符,将字符读入数组
  3. void close() 关闭流并释放资源
FileReader继承了Reader

构造方法 FileReader(String fileName) 参数是一个文件的路径

​ FileReader(File file) 参数是一个文件

字符输入流读取字符数据

import java.io.*;
public class ReaderDemo{
    public static void main(String[] args) throws IOException{
        //字符流的创建,需要绑定数据源
        //绑定了文件路径,也可以绑定文件
        FileReader fr = new FileReader("E:\\study\\demo13\\a.txt");
        //使用循环来读取文件
        /*int len = 0;
        //read(),返回的是读取到的字符
        while((len=fr.read())!=-1){
            System.out.println((char)len);
        }*/
        int len = 0;
        //试一下用数组来读取文件
        //定义一个字符数组,他是用来存储读取到的字符
        char[] cs = new char[1024];
        while((len=fr.read(cs))!=-1){
            //String的构造方法,可以把字符数组转换成字符串
            System.out.println(new String(cs,0,len));
        }
        fr.close();
    }
}

字符输出流的基本使用_写单个字符到文件

public static void main(String[] args) throws IOException{
        //创建FileWriter对象,绑定写入的数据目的地
        //如果没有文件会创建文件
        FileWriter fw = new FileWriter("E:\\study\\demo13\\b.txt");
        //write(int c)写入单个字符,
        fw.write(97);//这里只是把数据写入到内存缓冲区中
        //需要flush()把存在内存缓冲区的数据写入到文件中
        fw.flush();
        fw.close();
    }

flush方法和close方法的区别

flush:刷新缓冲区,流对象可以继续使用

close:先刷新缓冲区,通知系统释放资源,流对象不能再使用

字符输出流写数据的其他方法
import java.io.*;
public class WriterDemo1{
    public static void main(String[] args) throws IOException{
        FileWriter fw = new FileWriter("E:\\study\\demo13\\d.txt");
        //void write(char[] cbuf)可以写入字符数组
        char[] cs = {'云','想','衣','裳','花','想','容'};
        fw.write(cs);
        //写入字符数组的一部分,从索引2开始写,写四个字符
        fw.write(cs,2,4);
        //写字符串
        fw.write("春风扶槛露华浓");
        //写一部分字符串
        fw.write("若非群玉山头见",0,3);
        fw.close();
    }
}
字符输出流的续写和换行
public class WriteDemo2{
    public static void main(String[] args){
        //如若参数为false,则会覆盖源文件,为true会在源文件的后面续写
        FileWriter fw = new FileWriter("E:\\study\\demo13\\f.txt",true);
        fw.write("云想衣裳花想容");
        //换行符是\r\n
        fw.write("\r\n"+"春风扶槛露华浓");
        fw.write("\r\n"+"若非群玉山头见");
        fw.write("\r\n"+"会向瑶台月下逢");
        fw.close();
        //现在仿佛执行程序都不会覆盖源文件
    }
}
Properties集合存储数据

Properties类表示了一个持久的属性集,properties 可保存在流中或从流中加载

Properties集合是一个唯一和IO流相结合的集合

//PropertiesDemo集合的常见方法和使用
import java.util.*;
public class PropertiesDemo{
    public static void main(String[] args){
        //创建Properties集合的对象
        Properties prop = new Properties();
        //调用Properties的方法setProperty
        prop.setProperty("李白","18");
        prop.setProperty("杜甫","22");
        prop.setProperty("苏轼","25");
        //取出键
        Set<String> set = prop.stringPropertyNames();
        //遍历Set,通过K获取v
        for(String key : set){
            String value = prop.getProperty(key);
            System.out.println(key+" "+value);
        }
        
    }
}   

Properties集合中的方法store

void store(OutputStream out, String comments),字节输出流,不能写入中文

void store(Writer writer,String comments)字符输出流,可以写中文

import java.util.*;
import java.io.*;
public class PropertiesDemo{
    public static void main(String[] args) throws IOException{
        //创建Properties集合的对象
        Properties prop = new Properties();
        //调用Properties的方法setProperty
        prop.setProperty("李白","18");
        prop.setProperty("杜甫","22");
        prop.setProperty("苏轼","25");
        //Properties集合中的方法store,可以把集合中的临时数据写入到硬盘中去
        FileWriter fw = new FileWriter("E:\\study\\demo13\\e.txt");
        prop.store(fw,"save date");
        fw.close();
        
        
    }
}   

img

Properties集合中的方法load

可以使用Properties集合中的方法load,把硬盘中保存的文件,读取到集合中使用

void load(InputStream inStream)字节输入流,不能读取中文的键值对

void load(Reader reader)字符输入流,能读取含有中文的键值对

public static void main(String[] args) throws IOException{
        Properties prop = new Properties();
        prop.load(new FileReader("E:\\study\\demo13\\e.txt"));
        Set<String> set = prop.stringPropertyNames();
        for(String key :set){
            String value = prop.getProperty(key);
            System.out.println(key+"="+value);
        }
        
    }

BufferedOutputStream_字节缓冲输出流

BufferedOutputStream继承自OutputStream

BufferedOutputStream的构造方法:

BufferedOutputStream(OutputStream out):创建一个新的缓冲输入流,已将数据写入指定的底层输出流

BufferedOutputStream(OutputStream out,int size):创建一个新的缓冲输入流,已将数据写入指定的底层输出流,并且指定缓冲区的大小

import java.io.*;
public class BufferedOutputStreamDemo {
    public static void main(String[] args) throws IOException{
        FileOutputStream fos = new FileOutputStream("E:\\study\\demo13\\g.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bos.write("云想衣裳花想容".getBytes());
        bos.close();
    }
}

BufferedInputStream_字节缓冲输入流

import java.io.*;
public class BufferedInputStreamDemo {
    public static void main(String[] args) throws IOException{
        FileInputStream fis = new FileInputStream("E:\\study\\demo13\\g.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        int len=0;
        while((len=bis.read())!=-1){
            System.out.println((char)len);
        }
        bis.close();
    }
}

BufferedWriter_字符缓冲输出流

//这段代码在编译器不报错,在命令行一直报错
public static void main(String[] args) throws IOException{
        //创建字符缓冲输出流
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\study\\demo13\\h.txt"));
        //调用方法写入字符
        for(int i=0;i<10;i++){
            bw.write("云想衣裳花想容");
            bw.newLine();
        }
        //关闭流,同时flush把缓冲区的数据,刷新到文件中         
        bw.close();
    }
BufferedReader_字符缓冲输入流
 public static void main(String[] agrs) throws IOException{
        //创建一个BufferedReader对象
        BufferedReader br = new BufferedReader(new FileReader("E:\\study\\demo13\\h.txt"));
        //调用方法read或者readLine,readLine返回的是一行,没有就返回null
        String line;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }

        br.close();

    }

文本排序练习

img

import java.io.*;
import java.util.HashMap;
//把一个打乱序号的文本恢复正常的排序
public class CopyDemo{
    public static void main(String[] args) throws IOException{
        //分别存储序号和文本
        HashMap<String,String> hm = new HashMap<>();
        BufferedReader br = new BufferedReader(new FileReader("E:\\study\\demo13\\k.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\study\\demo12\\k.txt"));
        String line;
        while((line=br.readLine())!=null){
            //将字符串进行切割,返回的是一个字符串数组,分别有索引0和索引1
            //可以把0为序号,1为文本内容
            //调用字符串的切割文本方法
            String[] arr = line.split("\\.");
            //key值会自动排序
            hm.put(arr[0],arr[1]);
        }
        //keySet会把HashMap的键全部取出来,放到一个集合中
        //遍历这个集合就可以拿到key
        for(String key : hm.keySet()){
            String value=hm.get(key);
            line = key +"."+value;
            //读一行,写一行
            bw.write(line);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}
转换流
OutputStreamWriter:

构造方法

OutputStreamWriter(OutputStream out)

OutputFtreamWriter(OutputStream out, string CharseName)

参数OutputStream:字节输出流,可以用来写转换之后的字节到文件中

参数String charsetName:编码名称

使用步骤:

  1. 创建OutputStreamWrite对象,构造方法中传递字节输出流和编码名称

  2. 使用OutputStreamWriter对象中的方法write,把字符转换为字节存储缓冲区中

  3. 使用OutputStreamWriter对象中的方法flush

    import java.io.*;
    public class OutputStreamWriterDemo{
        public static void main(String[] args) throws IOException{
            GBK_write();
        }
        public static void utf_8_write() throws  IOException{
    
            //传递一个字节输出流的对象,默认编码表是utf-8
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\study\\demo13\\i.txt"));
            osw.write("云想衣裳花想容");
            osw.flush();
            osw.close();
        }
        public static void GBK_write() throws IOException {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\study\\demo13\\J.txt"),"GBK");
            osw.write("春风扶槛露华浓");
            osw.flush();
            osw.close();
        }
    }
    

    InputStreamReader类

    可以按照指定的编码表来解码

    构造方法:

    InputStreamReader(InputStream in)

    InputStreamReader(InputStream in,String charsetName)

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo {
    public static void main(String[] args) throws IOException {
        //read_utf_8();
        read_GBK();
    }

    private static void read_GBK() throws IOException {
        //读取GBK文件
        InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\study\\demo13\\J.txt"),"GBK");
        int len=0;
        while ((len=isr.read())!=-1){
            System.out.println((char)len);
        }
        isr.close();
    }


    private static void read_utf_8() throws IOException {
        //读取utf-8文件
        InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\study\\demo13\\i.txt"));
        int len=0;
        while ((len=isr.read())!=-1){
            System.out.println((char)len);
        }
        isr.close();
    }

}

猜你喜欢

转载自www.cnblogs.com/train99999/p/10928453.html