JAVA IO字符流 递归
目录[ Hide ]
字符流FileReader
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo1_FileReader {
public static void main(String[] args) throws IOException {
// demo1();
FileReader fr = new FileReader("xxx.txt");
int c ;
while((c = fr.read()) != -1){ //通过项目默认的码表一次读取一个字节
System.out.println((char)c);
}
}
private static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("xxx.txt");
int b = fr.read();
System.out.println(b);
char c = (char)b;
System.out.println(c);
fr.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Demo1_FileReader {
public static void main(String[] args) throws IOException {
// demo1();
FileReader fr = new FileReader("xxx.txt");
int c ;
while((c = fr.read()) != -1){ //通过项目默认的码表一次读取一个字节
System.out.println((char)c);
}
}
private static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("xxx.txt");
int b = fr.read();
System.out.println(b);
char c = (char)b;
System.out.println(c);
fr.close();
}
}
字符流FileWriter
package com.heima.chario;
import java.io.FileWriter;
import java.io.IOException;
public class Demo2_FileWriter {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("yyy.txt");
fw.write("大家好");
fw.close();
}
}
import java.io.FileWriter;
import java.io.IOException;
public class Demo2_FileWriter {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("yyy.txt");
fw.write("大家好");
fw.close();
}
}
字符流的拷贝
package com.heima.chario;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
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();
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
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();
}
}
什么情况下使用字符流
字符流是否可以拷贝非纯文本的文件
自定义字符数组的拷贝
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
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){ //将文件上的数据读取到字符数组中
fw.write(arr,0,len); //将字符数组中的数据拷贝到新文件上
}
fr.close();
fw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
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();
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
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){ //将文件上的数据读取到字符数组中
fw.write(arr,0,len); //将字符数组中的数据拷贝到新文件上
}
fr.close();
fw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
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();
}
}
带缓冲的字符流
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
// demo2();
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
int c;
while((c= br.read()) != -1){
bw.write(c);
}
br.close();
bw.close();
}
private static void demo2() throws FileNotFoundException, IOException {
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){ //将文件上的数据读取到字符数组中
fw.write(arr,0,len); //将字符数组中的数据拷贝到新文件上
}
fr.close();
fw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
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();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo3_Copy {
public static void main(String[] args) throws IOException {
// demo1();
// demo2();
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
int c;
while((c= br.read()) != -1){
bw.write(c);
}
br.close();
bw.close();
}
private static void demo2() throws FileNotFoundException, IOException {
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){ //将文件上的数据读取到字符数组中
fw.write(arr,0,len); //将字符数组中的数据拷贝到新文件上
}
fr.close();
fw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
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();
}
}
readLine()和newLine()方法
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4_Buffered {
/*
* newLine和\r\n区别
* newLine是跨平台的
* \r\n只支持windows系统
* */
public static void main(String[] args) throws IOException {
// demo1();
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
String line;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
private static void demo1() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
String line;
while((line = br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo4_Buffered {
/*
* newLine和\r\n区别
* newLine是跨平台的
* \r\n只支持windows系统
* */
public static void main(String[] args) throws IOException {
// demo1();
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("yyy.txt"));
String line;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
private static void demo1() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
String line;
while((line = br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
将文本反转
package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
//创建集合对象
ArrayList<String> list = new ArrayList<>();
//将读取到的数据存储在集合中
String line;
while((line = br.readLine()) != null){
list.add(line);
}
br.close();
//4.倒着遍历集合将数据写到文件上
BufferedWriter bw = new BufferedWriter(new FileWriter("fanzhuan.txt"));
for (int i = list.size()-1; i >=0; i--) {
bw.write(list.get(i));
bw.newLine();
}
bw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("xxx.txt"));
//创建集合对象
ArrayList<String> list = new ArrayList<>();
//将读取到的数据存储在集合中
String line;
while((line = br.readLine()) != null){
list.add(line);
}
br.close();
//4.倒着遍历集合将数据写到文件上
BufferedWriter bw = new BufferedWriter(new FileWriter("fanzhuan.txt"));
for (int i = list.size()-1; i >=0; i--) {
bw.write(list.get(i));
bw.newLine();
}
bw.close();
}
}
LineNumberReader
package com.heima.chario;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class Demo5_LineNumberReader {
public static void main(String[] args) throws IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("xxx.txt"));
String line;
lnr.setLineNumber(100);
while((line = lnr.readLine()) != null){
System.out.println(lnr.getLineNumber()+":"+line);
}
lnr.close();
}
}
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class Demo5_LineNumberReader {
public static void main(String[] args) throws IOException {
LineNumberReader lnr = new LineNumberReader(new FileReader("xxx.txt"));
String line;
lnr.setLineNumber(100);
while((line = lnr.readLine()) != null){
System.out.println(lnr.getLineNumber()+":"+line);
}
lnr.close();
}
}
装饰设计模式
package com.heima.chario;
public class Demo6_Wrap {
public static void main(String[] args) {
HeiMaStudent hms = new HeiMaStudent(new Student());
hms.code();
}
}
interface Coder{
public void code();
}
class Student implements Coder{
@Override
public void code() {
System.out.println("javase");
System.out.println("javaweb");
}
}
class HeiMaStudent implements Coder{
//获取被装饰类引用
private Student s; //获取学生引用
//在构造方法中传入被装饰类的对象
public HeiMaStudent(Student s){
this.s = s;
}
//对原有的功能进行升级
@Override
//低耦合
public void code() {
s.code();
System.out.println("ssh");
System.out.println("数据库");
System.out.println("大数据");
System.out.println("...");
}
}
public class Demo6_Wrap {
public static void main(String[] args) {
HeiMaStudent hms = new HeiMaStudent(new Student());
hms.code();
}
}
interface Coder{
public void code();
}
class Student implements Coder{
@Override
public void code() {
System.out.println("javase");
System.out.println("javaweb");
}
}
class HeiMaStudent implements Coder{
//获取被装饰类引用
private Student s; //获取学生引用
//在构造方法中传入被装饰类的对象
public HeiMaStudent(Student s){
this.s = s;
}
//对原有的功能进行升级
@Override
//低耦合
public void code() {
s.code();
System.out.println("ssh");
System.out.println("数据库");
System.out.println("大数据");
System.out.println("...");
}
}
使用指定的码表读写字符
package com.heima.chario;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Demo7_TransIO {
public static void main(String[] args) throws IOException {
// demo1(); //默认码表读写出现乱码
// demo2();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk"));
int c;
while((c = br.read()) != -1){
bw.write(c);
}
br.close();
bw.close();
}
private static void demo2() throws UnsupportedEncodingException, FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");
int c;
while((c = isr.read()) != -1){
osw.write(c);
}
isr.close();
osw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("utf-8.txt");
FileWriter fw = new FileWriter("gbk.txt");
int c;
while((c= fr.read()) != -1){
fw.write(c);
}
fr.close();
fw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Demo7_TransIO {
public static void main(String[] args) throws IOException {
// demo1(); //默认码表读写出现乱码
// demo2();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk"));
int c;
while((c = br.read()) != -1){
bw.write(c);
}
br.close();
bw.close();
}
private static void demo2() throws UnsupportedEncodingException, FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("utf-8.txt"),"utf-8");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("gbk.txt"),"gbk");
int c;
while((c = isr.read()) != -1){
osw.write(c);
}
isr.close();
osw.close();
}
private static void demo1() throws FileNotFoundException, IOException {
FileReader fr = new FileReader("utf-8.txt");
FileWriter fw = new FileWriter("gbk.txt");
int c;
while((c= fr.read()) != -1){
fw.write(c);
}
fr.close();
fw.close();
}
}
转换流图解
获取文本上字符出现的次数
package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
public class Test3 {
/*获取一个文本上每个字符出现的次数,将结果写在times.txt上
*
* 1.创建带缓冲的输入流对象
* 2.创建双列集合对象TreeMap
* 3.将读到的字符存储在双列集合中,存储的时候要做判断,如果不包含这个键,就将键和1存储,如果包含这个键,就将该键和值加1存储
* 4.关闭输入流,创建输出流对象
* 5.遍历集合将集合中的内容写到times.txt中
* 6.关闭输出流
* */
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("fanzhuan.txt"));
TreeMap<Character, Integer> tm = new TreeMap<>();
int ch;
while((ch = br.read())!= -1){
char c = (char)ch; //强制类型转换
/*if(tm.containsKey(c)){
tm.put(c, 1);
}else{
tm.put(c, tm.get(c)+1);
}*/
tm.put(c, !tm.containsKey(c)? 1 : tm.get(c)+1);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
for( Character key : tm.keySet()){
switch (key) {
case '\t':
bw.write("\\t" + "=" + tm.get(key));
break;
case '\n':
bw.write("\\n" + "=" + tm.get(key));
break;
case '\r':
bw.write("\\r" + "=" + tm.get(key));
break;
default:
bw.write(key + "=" + tm.get(key));
break;
}
bw.newLine();
}
bw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.TreeMap;
public class Test3 {
/*获取一个文本上每个字符出现的次数,将结果写在times.txt上
*
* 1.创建带缓冲的输入流对象
* 2.创建双列集合对象TreeMap
* 3.将读到的字符存储在双列集合中,存储的时候要做判断,如果不包含这个键,就将键和1存储,如果包含这个键,就将该键和值加1存储
* 4.关闭输入流,创建输出流对象
* 5.遍历集合将集合中的内容写到times.txt中
* 6.关闭输出流
* */
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("fanzhuan.txt"));
TreeMap<Character, Integer> tm = new TreeMap<>();
int ch;
while((ch = br.read())!= -1){
char c = (char)ch; //强制类型转换
/*if(tm.containsKey(c)){
tm.put(c, 1);
}else{
tm.put(c, tm.get(c)+1);
}*/
tm.put(c, !tm.containsKey(c)? 1 : tm.get(c)+1);
}
br.close();
BufferedWriter bw = new BufferedWriter(new FileWriter("times.txt"));
for( Character key : tm.keySet()){
switch (key) {
case '\t':
bw.write("\\t" + "=" + tm.get(key));
break;
case '\n':
bw.write("\\n" + "=" + tm.get(key));
break;
case '\r':
bw.write("\\r" + "=" + tm.get(key));
break;
default:
bw.write(key + "=" + tm.get(key));
break;
}
bw.newLine();
}
bw.close();
}
}
试用版软件
package com.heima.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test4 {
/*
* */
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
String line = br.readLine();
int times = Integer.parseInt(line);//将数字字符串转换为数字
if(times >0){
System.out.println("您还有"+times-- + "次机会");
FileWriter fw = new FileWriter("config.txt");
fw.write(times + "");
fw.close();
}else{System.out.println("您的试用次数已到,请购买正版");}
br.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test4 {
/*
* */
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
String line = br.readLine();
int times = Integer.parseInt(line);//将数字字符串转换为数字
if(times >0){
System.out.println("您还有"+times-- + "次机会");
FileWriter fw = new FileWriter("config.txt");
fw.write(times + "");
fw.close();
}else{System.out.println("您的试用次数已到,请购买正版");}
br.close();
}
}
递归
package com.heima.chario;
public class Demo8_Digui {
/*递归:方法自己调用自己
* 弊端:不能调用次数过多,容易导致栈内存溢出
* 好处:不用知道循环次数
* */
public static void main(String[] args) {
/*int result = 1;
for(int i = 1; i<=5;i++){
result = result *i;
}
System.out.println(result);*/
System.out.println(fun(5));
}
public static int fun(int num){
if(num == 1){
return 1;
}
else{
return num * fun(num -1);
}
}
}
public class Demo8_Digui {
/*递归:方法自己调用自己
* 弊端:不能调用次数过多,容易导致栈内存溢出
* 好处:不用知道循环次数
* */
public static void main(String[] args) {
/*int result = 1;
for(int i = 1; i<=5;i++){
result = result *i;
}
System.out.println(result);*/
System.out.println(fun(5));
}
public static int fun(int num){
if(num == 1){
return 1;
}
else{
return num * fun(num -1);
}
}
}