1.将前面作业中关于文件拷贝的操作,替换为高效流
package work;
/*
* 1.将前面作业中关于文件拷贝的操作,替换为高效流
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Work01 {
public static void main(String[] args) throws IOException{
File file = new File("C:\\Users\\ZYX'sDay\\Desktop\\FileDemo");
File file1 = new File(file,"ss");
File file2 = new File(file,"sfsdf");
fileCopy(file1,file2);
}
private static void fileCopy(File file1, File file2) throws IOException{
FileInputStream fis = null;
FileOutputStream fos = null;
File[] files = file1.listFiles();
for (File file : files) {
if(file.isDirectory()){
fileCopy(file, file2);
}else {
//转换高效 缓冲流
fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
fos = new FileOutputStream(new File( file2, file.getName() ) );
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[1024];
int len = 0;
while( (len = bis.read(bytes)) != -1 ){
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
}
}
}
}
2. 用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
package work;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
/*
* 2. 用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
(2)打印格式:
to=3
think=1
you=2
//........
(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
*/
public class Work02 {
public static void main(String[] args) throws IOException{
String str = "If you want to change your fate I think you must come to the dark horse to learn java";
FileWriter fw = new FileWriter("D:\\day23worktest\\buffer\\count.txt");
BufferedWriter bw = new BufferedWriter(fw);
//用正则表达式拆分字符串 空格为标志
String[] strings = str.split(" ");
HashMap<String, Integer> hMap = new HashMap<>();
Object[] key_arr = null;
//双层循环 遍历匹配
for (int i = 0; i < strings.length-1; i++) {
int count = 0;
for(int j = 1 ; j < strings.length-1 ;j++){
if( strings[i].equals(strings[j]) ){
count++;
}
}
hMap.put(strings[i], count);
}
//整理顺序
key_arr = hMap.keySet().toArray();
Arrays.sort(key_arr);
for(Object key : key_arr){
bw.append(key.toString());
bw.append('=');
bw.append(hMap.get(key).toString());
bw.newLine();
bw.flush();
}
bw.close();
}
}
3.利用转换流将GBK格式文件以UTF-8输出
package work;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//3.利用转换流将GBK格式文件以UTF-8输出
public class Work03 {
public static void main(String[] args) throws IOException{
File file = new File("C:\\Users\\ZYX'sDay\\Desktop\\FileDemo");
File file1 = new File(file, "GBK.txt");
File file2 = new File(file,"utfs8.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
//创建转换流
InputStreamReader isr = new InputStreamReader(fis, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
char[] ch = new char[1024];
int len = isr.read(ch);
System.out.println(new String(ch,0,len));
osw.write(ch, 0, len);
isr.close();
osw.close();
}
}
4.java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
package work;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
//4.java写一个程序,实现从文件中读出文件内容,
//并将其打印在屏幕当中,并标注上行号
public class Work04 {
public static void main(String[] args) throws IOException{
File file = new File("D:\\day23worktest\\buffer\\count.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
//建读行方法
LineNumberReader lnr = new LineNumberReader(br);
String line = null;
while( (line = lnr.readLine()) != null ){
System.out.println(lnr.getLineNumber()+". "+line);
}
br.close();
}
}