IO Exercise
1.转码:
转换GBK编码的文本文件,转换为UTF-8编码的文本文件
- 分析
1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
2.创建OutputStreamwriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
3.使用InputStreamReader对象中的方法read读取文件
4.使用OutputStreamwriter对象中的方法write,把读取的数据写入到文件中
5.释放资源
public class FileGBKtranslateUTF {
public static void main(String[] args) throws IOException {
translategu();
}
private static void translategu() throws IOException {
InputStreamReader ir=new InputStreamReader(new FileInputStream("cn\\itcast\\IO\\OutputStreamWriterGbk.txt"),"gbk");
OutputStreamWriter ow=new OutputStreamWriter(new FileOutputStream("cn\\itcast\\IO\\GBKtranslateUTF.txt"));
int len ;
while((len=ir.read())!=-1)
{
ow.write(len);
}
ow.close();
ir.close();
}
}
2.排序
分析
- 1.创建一个HashMap集合,可以:存储每行文本的序号(1,2,3,4,5,6):value:存储每行的文本
- 2.创建字符缓冲输入对象,构造方法中绑定字符输入流
- 3.创建字符缓冲输出对象,构造方法中绑定字符输出流
- 4.使用字符缓冲输入流中的readLine方法,逐行读取文本
- 5.对读取的文本进行切隔,获取行中的序号和文本内容
- 6.把切割好的序号和文本存储到HashMap集合中(key序号是有序的,自动排序)
- 7.遍历HashMap集合,获取每一个键值对
- 7.把每一个键值对,拼接为一个文本行
- 9.把拼接好的文本,使用字符缓冲输出流中的方法write,写入到文件中
- 10.释放资源
public class FilePaixu {
public static void main(String[] args) throws IOException {
//1.创建一个HashMap集合,可以:存储每行文本的序号(1,2,3,4,5,6):value:存储每行的文本
Map<String,String> map=new HashMap<>();
//2.创建字符缓冲输入对象,构造方法中绑定字符输入流
//3.创建字符缓冲输出对象,构造方法中绑定字符输出流
BufferedReader br=new BufferedReader(new FileReader("cn\\itcast\\IO\\write.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("cn\\itcast\\IO\\Paixuout.txt"));
//4.使用字符缓冲输入流中的readLine方法,逐行读取文本
String line;
while ((line=br.readLine())!=null)
{
String[] array = line.split("\\.");
map.put(array[0],array[1]);
}
//循环写入
for (String key:map.keySet()
) {
String value=map.get(key);
line=key+"."+value;
bw.write(line);
bw.newLine();//转行
}
/* Set<String> key = map.keySet();
Object[] arr = key.toArray();
String[] str=new String[arr.length];
//int[] ints=new int[str.length];int k=0;
*//* for (Object ars:arr)
{
ints[k]=(int)ars;
System.out.println(ints[k]);
k++;
}*//*
for (int i = 0; i < arr.length; i++) {
str[i]= (String) (arr[i]);
// ints[i]=(int)arr[i];不可以,归根结底这个是String类型的,不可以转换为int型的
//System.out.println(ints[i]);
}
Arrays.sort(str);
*//*Arrays.sort(ints);
System.out.println(ints.toString());*//*
for (String strs:str
) {
System.out.println(strs);
String value=map.get(strs);
line=strs+"."+value;
bw.write(line);
bw.newLine();//转行
System.out.println("-----------------------------");
}
for (String keys:key
) {
System.out.println(keys);
String value=map.get(keys);
line=keys+"."+value;
bw.write(line);
bw.newLine();//转行
}*/
bw.close();
br.close();
}
}
3.复制
文件复制,步骤
- 1.创建文件的读对象
- 2.创建文件的写对象
- 3.使用循环,读取一个字节写入一个字节,或者读取一组字节,写入一组字节
- 4.释放资源(先关写的,再关读的)
public class FilePaste {
public static void main(String[] args)throws IOException {
//1.创建文件的读对象
//* 2.创建文件的写对象
long s = System.currentTimeMillis();
FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\InputStream读取原理.png");
FileOutputStream fos=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\InputStream读取原理1.png");
/*int len=0;
while ((len=fis.read())!=-1)
{
//3.使用循环,读取一个字节写入一个字节,或者读取一组字节,写入一组字节
fos.write(len);
}*/
byte bytes[]=new byte[1024];
int len;
while ((len=fis.read(bytes))!=-1)
{
//3.使用循环,读取一个字节写入一个字节,或者读取一组字节,写入一组字节
fos.write(bytes);
}
//4.释放资源(先关写的,再关读的)
fos.close();
fis.close();
long e = System.currentTimeMillis();
System.out.println("复制文件用时:"+(e-s)+"毫秒");//方法一,用一个字节复制。方法二用一组字节复制,复制文件用时:13毫秒
}
}
3.2Buffered复制
文件复制,步骤
- 1.创建字节缓冲输入流对象,构造方法中传递字节输入流
- 2.创建字节缓冲输出流对象,构造方法中传递字节输出流
- 3.使用字节缓冲输入流对象中的read,读取文件
- 4.使用字节输出流对象中的write,把读取的文件的数据写入到内部缓冲区中
- 5.释放资源(会先把缓冲区中的数据,刷新到文件中)
public class FilePasteBuffered {
public static void main(String[] args)throws IOException {
//1.创建字节缓冲输入流对象,构造方法中传递字节输入流
// * 2.创建字节缓冲输出流对象,构造方法中传递字节输出流
long s = System.currentTimeMillis();
FileInputStream fis=new FileInputStream("C:\\Users\\Administrator\\Desktop\\InputStream读取原理.png");
FileOutputStream fos=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\InputStream读取原理1.png");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte bytes[]=new byte[1024];
int len;
while ((len=bis.read(bytes))!=-1)
{
//3.使用循环,读取一个字节写入一个字节,或者读取一组字节,写入一组字节
bos.write(bytes);
}
//4.释放资源(先关写的,再关读的)
bos.close();
fos.close();
long e = System.currentTimeMillis();
System.out.println("复制文件用时:"+(e-s)+"毫秒");//方法一,用一个字节复制。方法二用一组字节复制,复制文件用时:13毫秒
}
}