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];
// String s = "";
int length = -1;
while ((length = is.read(buffer)) != -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();
}
}
原文地址:http://www.cnblogs.com/wyzel/p/16819883.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性