Hello friend, I'm|

Ritchie里其

园龄:2年6个月粉丝:4关注:7

练习题08File、IO

1、创建一个文件文件中的内容是
name=张三
age =12 pwd=234
读取文件在控制台打印
张三
12
234

package com.xxx;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class Homework1 {
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("d:\\a\\b\\zhangsan.txt");
byte[] buffer = new byte[1024];//存储读取到的数据 缓冲数组 的长度一般是1024的倍数,因为与计算机的处理单位。 理论上缓冲数组越大,效率越高
// String s = "";
int length = 0;//保存每次读取到的字节个数。
while ((length = is.read(buffer)) != -1) {//read方法如果读取到了文件的末尾,那么会返回-1表示。
// System.out.println(new String(buffer, 0, length));
String s = new String(buffer, 0, length);
String[] split = s.split(" ");
for (int i = 0; i < split.length; i++) {
int index = split[i].indexOf("=");
String str = split[i].substring(index + 1);
System.out.println(str);
}
// s += (char)length + "";
// System.out.println(s);
}
is.close();
}
}

2、使用代码在创建这个一个文件d: \admin\dd\1. txt
向文件中写入admin 12345
再将文件中的内容读取出来只将admin写入到 d:\a\b\2. txt中

package com.xxx;
import java.io.*;
public class Homework2 {
public static void main(String[] args) throws IOException {
File file = new File("d:\\a\\b\\111.txt");
file.createNewFile();
String s = "admin 12345";
byte[] bytes = s.getBytes();
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream("d:\\a\\b\\222.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);
os.write(bytes);
int length = -1;
while ((length = bis.read()) != -1) {
bos.write(length);
}
}
}

3、创建一个文件文件中的内容adhdheeuyrydhhddgddhdhddddsvsjssjsjdhdfbfd 将使用高效流读取文件中的内容
存入到1ist集合中统计出a=3 b=2

package com.xxx;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Homework3 {
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("d:\\a\\b\\new.txt");
BufferedInputStream bis = new BufferedInputStream(is);
List<Character> list = new ArrayList<Character>();
int length = -1;
while ((length = bis.read()) != -1) {
// System.out.print((char)length);
list.add((char)length);
}
// System.out.println(list);
countOfList(list);
}
public static Map<Character,Integer> countOfList(List<Character> list) {
Map<Character, Integer> map = new HashMap<>();
list.forEach(c -> {Integer counts = map.get(c);map.put(c,counts == null ? 1 : ++counts); });
System.out.println(map);
return map;
}
}

4、将c盘的音频复制到d盘中

package com.xxx;
import java.io.*;
public class Homework4 {
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("d:\\a\\b\\old.mp3");
BufferedInputStream bis = new BufferedInputStream(is);
OutputStream os = new FileOutputStream("d:\\a\\old.mp3");
BufferedOutputStream bos = new BufferedOutputStream(os);
int length = -1;
while ((length = bis.read()) != -1) {
bos.write(length);
}
bos.close();
bis.close();
os.close();
is.close();
}
}

本文作者:Ritchie里其

本文链接:https://www.cnblogs.com/wang-zeyu/p/16819883.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ritchie里其  阅读(29)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
✨欢迎你~🍻
✨欢迎你~🍻
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 遥か Aimer
遥か - Aimer
00:00 / 00:00
An audio error has occurred.

作词 : aimerrhythm/田中ユウスケ

作曲 : 田中ユウスケ

编曲 : 玉井健二/百田留衣

海岸線の雨に ちらばった君の影

思い出が交差する 海辺の街

君はあの日のまま いまも夢を見てた

君はあの日のまま いまも夢を見てた

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

膝までの浅瀬で 見つけた星

君まで届くなんてさ ありえないような

浅い眠りの中で 深い夢から覚めて

浅い眠りの中で 深い夢から覚めて

裸足のまま駆けてく まばゆい星

君はあの日のまま どんな夢を見てた?

君はあの日のまま どんな夢を見てた?

遥か記憶の空 2人照らす光

遥か記憶の空 2人照らす光

いつまでもこうして 笑っててほしい

夜空に舞い上がる 幾千の花びら

でたらめな誓いで 生きてく日々

君から届くなんてさ ありえないような