美文网首页
Java基础笔记总结(14)-IO流(2)字符流 FileRea

Java基础笔记总结(14)-IO流(2)字符流 FileRea

作者: 吵吵先生 | 来源:发表于2019-01-29 18:42 被阅读0次

字符流是什么

字符流是可以读写字符的IO流

字符流读取字符,先要读取字节数据,然后转换为字符,需要把字符转换为字节再写出

FileReader类的read()方法可以按照字符大小读取数据

FileReader  fr = new FileReader("aaa.txt");//注意 这个文件必须存在

int ch;

while((ch = fr.read())!=-1){//通过项目默认的码表一次读取一个字符

  System.out.println((char)ch);

}

fr.close();

---------------------------------------------------------------------------------

FileWriter

FileWriter fw = new FileWriter("yyy.txt");

fw.write(Str);

fw.close();

---------------------------------------------------------------------------------

字符流的拷贝

FileReader fr = new FileReader("xxx.txt");

FileWriter fw = new FileWriter("zzz.txt");

int c;

while((c = fr.read())!=-1){

fw.write(c);

}

fr.close();

fw.close();

字符流可以拷贝文本文件,但是不推荐使用,因为会先将字节变为字符读取,而写入会将字符变成字节在写入。

如果按照字符读取,不会出现半个中文的状况,

但是拷贝图片不建议使用,因为拷贝过程中,字节找不到对应的字符,就会用?代替,在写入过程中,再讲字符转换成字节写出去,这样文件就乱了。

----------------------------------------------------------------------------------

自定义字符数组的拷贝

FileReader fr = new FileReader("xxx.txt");

FileWriter fw = new FileWriter("zzz.txt");

char[] arr = new char[1024];

int len;

while((len = fr.read(arr))!=-1){

fr.writer();

}

--------------------------------------------------------------------------------------

IO缓冲流

BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));

BufferedWriter bw = new BufferedWriter(new FileWriter("xxx.txt"));

int c;

while((c = br.read())!=-1){

    br.write(c);

}

br.close();

bw.close();

BufferedReader readLine();

BufferedWriter newLine();读取但不会 是换行符(跨平台使用)

-------------------------------------------------------------------------------------

文本反转(行数反转) 流对象尽量晚开早关  Collections.reverse(list);也可以实现集合反转

BufferedReader

String line;

while((line = br.readLine())!= null){

  list.add(line);

}

for(int i = list.size()-1;i>=0;i--){

bw.write(list.get(i));

bw.newLine();

}

----------------------------------------------------------------------------

行号的设置和获取

setLineNumber

getLineNumber

LineNumberReader lnr = new LineNumberReader(new FileReader("zzz.txt"));

String line;

lnr.setLineNumer(100);

while((line = lnr.readLine())!=null){

System.out.println(lnr.getLineNumber()+line);

}

lnr.close();

--------------------------------------------------------------------------

设计模式(装饰设计模式)原来功能不强大,设计将其来更强大--------------------降低了耦合性

public class Demo_Wrap {

public static void main(String[] args) {

HeiMaStudent heiMaStudent = new HeiMaStudent(new Student());

heiMaStudent.code();

}

}

interface Coder{

void code();

}

class Student implements Coder{

s

@Override

public void code() {

System.out.println("javase");

System.out.println("javaweb");

}

}

class HeiMaStudent implements Coder{

// 1、获取引用

private Student s;

// 2、在构造方法中传入被装饰类对象

public HeiMaStudent(Student s) {

this.s = s;

}

// 对原有功能进行升级

@Override

public void code() {

s.code();

System.out.println("ssh");

System.out.println("数据库");

}

}

----------------------------------------------------------------------------------

使用指定码表读写字符

FileReader fr = new FileReader("");

FileWriter fw = new FileWriter("");

InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"),"UTF-8");

OutputStreamWriter osw =  new OutputStreamWriter(new FileOutputStream("xxx.txt"),"UTF-8");

int c ;

while((c=isr.read())!=-1){

osw.write(c);

}

isr.close();

osr.close();

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("")),"UTF-8");

int c ;

while((c=br.read())!=-1){

bw.write(c);

}

----------------------------------------------------------------------------------

获取文本上字符做的次数

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.TreeMap;

public class Demo_CopyNum {

public static void main(String[] args) throws IOException {

BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));

TreeMap<Character, Integer> tm = new TreeMap<>();

int ch;

while ((ch = br.read()) != -1) {

char c = (char) ch;

tm.put(c, !tm.containsKey(c) ? 1 : tm.get(c) + 1);

}

br.close();

BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));

for (Character key : tm.keySet()) {

bw.write(key+"="+tm.get(key));

bw.newLine();

}

bw.close();

}

}

-----------------------------------------------------------------------------------

适用版本软件

BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));

String line = br.readLine();为了保证数据的原样性

int times = Integer.parseInt(line);

if(times> 0)}{

System.out.println(timer--);

FileWriter fw = new FileWriter("config.txt");

fw.write(times+"");

fw.close();//必须要关 否者只在缓冲区,并没有写入到文件中

}else{

}

br.close();

--------------------------------------------------------------------------------------

File递归

注意 构造方法是不可以递归调用的

递归调用次数过多会出现栈内存的溢出

递归调用可以有返回值,也可以没有返回值

-------------------------------------------------------------------------------------

从键盘接受一个文件夹路径,遍历所有点java文件

while(true){

String line = sc.nextLine();

File dir = new File(line);

if(!dir.exists()){

System.out.println()

}else if(dir.isFile()){

}else{

return dir;

}

main :

File dir = getDir();

获取文件夹下的所有.java文件

public static void printFile(File dir){

File[] subFiles = dir.listFiles();

for(File fire : subFiles){

if(fire.iseFile()&&fire.getName().endsWith("java"))

}else if(dir.isDirectory){

printFile(subFile);

}

}

相关文章

网友评论

      本文标题:Java基础笔记总结(14)-IO流(2)字符流 FileRea

      本文链接:https://www.haomeiwen.com/subject/snxfsqtx.html