Java学习32天---字符编码,字符流,字符节点,过滤流,桥转换,File对象

千锋逆战班:孙华建
在千锋学习第32天
“未来的你会感谢今天奋斗的自己”
今天我学习了java课程,字符编码,字符流,字符节点,过滤流,桥转换,File对象
#中国加油!武汉加油!千锋加油!也为自己加油!!!#…

笔记
在这里插入图片描述
课堂案例
1,TestEncoding


import java.io.UnsupportedEncodingException;

public class TestEncoding {

	public static void main(String[] args) throws UnsupportedEncodingException {
		String str = "你好世界123abc喆";
		
		//编码
		byte[] bs = str.getBytes("GBK");
		
		for (int i = 0; i < bs.length; i++) {
			System.out.println(bs[i]);
		}
		//解码
		String str2 = new String(bs,"BIG5");
		System.out.println(str2);
		
		byte[] bs1 = str2.getBytes("BIG5");
		String str3 = new String(bs1,"GBK");
		
		System.out.println(str3);
		
		String s ="你好陶喆abc123";
		byte[] b = s.getBytes("UTF-8");
		String s1 = new String(b,"UTF-8");
		System.out.println(s1);
	}

}

2,TestBuffered


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class TestBuffered {

	public static void main(String[] args) throws Exception {
		FileWriter fw = new FileWriter("Files\\buf.txt");
//		BufferedWriter bw = new BufferedWriter(fw);
//		
//		bw.write("Hello World");
//		
//		bw.newLine();
//		
//		bw.write("Hello java");
//		
//		bw.flush();
		
		//过滤流,比BufferedWriter更方便
		PrintWriter pw = new PrintWriter(fw);
		
		pw.println("Hello");
		
		pw.println("World");
		
		pw.println("GoodBye");
		
		pw.flush();
		
		//------字符输入过滤流
		FileReader fr = new FileReader("Files\\buf.txt");
		BufferedReader br = new BufferedReader(fr);
		//BufferedReader.readLine()返回的是null,不是-1
		while(true){
			String s = br.readLine();
			if(s == null){
				break;
			}
			System.out.println(s);
		}
		
	}

}

3,TestConvertStream


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class TestConvertStream {

	public static void main(String[] args) throws Exception {
		OutputStream os = new FileOutputStream("Files\\convert.txt");
		OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
		PrintWriter pw = new PrintWriter(osw);
		
//		osw.write("天气真好");
//		osw.flush();
		pw.println("你好世界");
		pw.println("你好java");
		pw.println("你好web");
		pw.flush();
		
		InputStream is = new FileInputStream("Files\\convert.txt");
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		
		while(true){
			String s =br.readLine();
			if(s==null){
				break;
			}
			System.out.println(s);
		}
		br.close();
		
//		char[] c = new char[4];
//		isr.read(c);
//		
//		for (int i = 0; i < c.length; i++) {
//			System.out.print(c[i]);
//		}
	}

}

4,TestWriter

import java.io.FileReader;
import java.io.FileWriter;

public class TestWriter {

public static void main(String[] args) throws Exception {
	FileWriter fw = new FileWriter("Files\\chars.txt");
	
	fw.write("Hello今天天气好65");
	char[] chars = new char[]{'你','好','吗'};
	
	fw.write(chars);
	fw.flush();
	
	FileReader fr = new FileReader("Files\\chars.txt");
	
	char[] c = new char[4];
	
	while(true){
		int n = fr.read(c);
		if(n == -1){
			break;
		}
		for (int i = 0; i < n; i++) {
			System.out.print(c[i]);
		}
		System.out.println();
	}
}

}
5,TestFiles


import java.io.File;
import java.io.IOException;

public class TestFiles {

	public static void main(String[] args) throws Exception {
		File file = new File("Files\\test\\target.txt");
//		
//		System.out.println(file.canExecute());
//		System.out.println(file.canWrite());
//		System.out.println(file.canRead());
//		
//		
//		System.out.println(file.delete());
//		Thread.sleep(2000);
//		file.deleteOnExit();
//		System.out.println(file.exists());
		
//		System.out.println(file.getAbsolutePath());
//		System.out.println(file.getPath());
//		System.out.println(file.getName());
//		System.out.println(file.getFreeSpace()/1024/1024/1024);
//		System.out.println(file.getTotalSpace()/1024/1024/1024);
//		System.out.println(file.getParent());
//		System.out.println(file.isDirectory());
//		System.out.println(file.isFile());
//		System.out.println(file.isHidden());
//		System.out.println((System.currentTimeMillis()-file.lastModified())/1000/60);
//		System.out.println(file.length());
		
//		file.mkdirs();
		System.out.println(file.createNewFile());
	}

}

6,TestListFiles


import java.io.File;
import java.io.FileFilter;

public class TestListFiles {

	public static void main(String[] args) {
		File file = new File("D:\\课堂案例\\day10");
//		
//		String[] fn = file.list();//获取文件夹里所有文件文件夹的名字
//		for (String s : fn) {
//			System.out.println(s);
//		}
		
		File[] files = file.listFiles(new MyFilter());
		for (File f : files) {
			System.out.println(f.getName());
		}

		
	}

}
class MyFilter implements FileFilter{

	@Override
	public boolean accept(File pathname) {
		if(pathname.isFile()){
			if(pathname.getName().endsWith(".doc")){
				return true;
			}
		}
		return false;
	}
	
}

7,TestShowAllFiles


import java.io.File;
import java.io.FileFilter;

public class TestShowAllFiles {
	static int count = 0;
	
	public static void main(String[] args) {
		File file = new File("D:\\");
		showall(file);
		System.out.println("一共"+count+"个文件");
	}
	
	public static void showall(File dir){
		
		File[] files = dir.listFiles(new FileFilter(){

			@Override
			public boolean accept(File f) {
				
				if(f.isDirectory()){
					return true;
				}
				
				if(f.isFile()){
					if(f.getName().endsWith(".class")){
						return true;
					}
				}
				return false;
			}			
		});
		
		if(files!=null){
			for (File file : files) {
				if(file.isFile()){
					System.out.println(file.getName());
					count++;
				}else{
					showall(file);
				}
				
			}
		}
	}

}

作业
1,
在这里插入图片描述
B
2,
在这里插入图片描述

import java.io.File;

public class TestMyFile {

	public static void main(String[] args) {
		
		File file;
		
		file = new File("hello.txt");
		
		System.out.println(file.exists());
		System.out.println(file.getAbsolutePath());

	}

}

10,
在这里插入图片描述
A获得的Writer只具备基本的方法,B则可以通过println()方法实现分行输出,C可以将字节流转换为字符流并规定字符编码集

11,
在这里插入图片描述
都可以使用字节流,I,II可以用字符流

12,
在这里插入图片描述
ObjectInputStream–读对象
ObjectOutputStream–写对象
BufferedInputStream–缓冲功能
BufferedOutputStream–字节流
DataInputStream–读八种基本类型
DataOutputStream–写八种基本类型
PrintWriter–写字符串并换行
PrintStream–字符流
BufferedReader–读一行文本

14,
在这里插入图片描述


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class TestFourteen {

	public static void main(String[] args) throws Exception {
		
		InputStream is = new FileInputStream("Files\\test.txt");
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		
		String s1 = br.readLine();
		String s2 = br.readLine();
		String s3 = br.readLine();
		String s4 = br.readLine();
		br.close();
		
		OutputStream os = new FileOutputStream("Files\\test2.txt");
		OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
		PrintWriter pw = new PrintWriter(osw); 
		
		pw.println(s4);
		pw.println(s3);
		pw.println(s2);
		pw.println(s1);
		
		pw.close();
		
	}

}

17,
在这里插入图片描述


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;

public class TestWoldcup {

	public static void main(String[] args) throws Exception {
		
		InputStream is = new FileInputStream("worldcup.txt");
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		
		HashMap<String,String> hm = new HashMap<String,String>();
		String[] s = new String[2];
		String str = br.readLine();

		while(str != null){
			s=str.split("/");
			hm.put(s[0], s[1]);
		}
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个年份:");
		String year = input.next();
		
		if(hm.keySet().toString().contains(year) == true){
			System.out.println("该年冠军是"+hm.get(year));
		}else{
			System.out.println("该年没有举办世界杯");
		}
	}

}

18,
在这里插入图片描述


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class TestEighteen {

	public static void main(String[] args) throws Exception {
		
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一个文件名");
		String s = input.next();
		
		File f = new File(s);
		
		InputStream is = new FileInputStream(s);
		InputStreamReader isr = new InputStreamReader(is,"UTF-8");
		BufferedReader br = new BufferedReader(isr);
		
		OutputStream os = new FileOutputStream(f.getParent()+"//copy_"+f.getName());
		OutputStreamWriter osw = new OutputStreamWriter(os,"UTF-8");
		PrintWriter pw = new PrintWriter(osw);
		
		if(f.isFile()){
			File f2 = new File(f.getParent()+"//copy_"+f.getName());
			f2.createNewFile();
			while(true){
				String s1 = br.readLine();
				if(s1==null){
					break;
				}
				pw.println(s1);
			}
			pw.close();
			
		}
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值