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();
}
}
标签:练习题,java,String,length,IO,import,new,08File,public
From: https://www.cnblogs.com/wyzel/p/16819883.html