目录
前言
这是我从网上看到的一些题目,自己写了代码,但是只注重了功能,可能有些代码的质量不高,有时间再优化或者修改吧。
题目1
- 利用字节输出流一次写一个字节的方式,向D盘的a.txt文件输出字符‘a’
代码如下:
public class test01 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt");
fileOutputStream.write(88);
fileOutputStream.close();
}
}
题目2
- :利用字节输出流一次写一个字节数组的方式向D盘的b.txt文件输出内容:“i love java”
代码如下:
import java.io.FileOutputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt");
byte[] b = "i love java".getBytes();
fileOutputStream.write(b);
fileOutputStream.close();
}
}
题目3
- 有一c.txt 文件中内容为:HelloWorld
在c.txt文件原内容基础上,添加五句 I love java,而且要实现一句一行操作(注:原文不可覆盖)。
代码如下:
public class Test03 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.txt",true);
byte[] b = "i love java\r\n".getBytes();
byte[] a = "\r\n".getBytes();
fileOutputStream.write(a);
for(int i = 0;i < 5;i++){
fileOutputStream.write(b);
fileOutputStream.flush();
}
fileOutputStream.close();
}
}
题目4
- 利用字节输入流读取文件a.txt的内容,文件内容确定都为纯ASCII字符
,使用循环读取,一次读取一个字节,直到读取到文件末尾。将读取的字节输出到控制台
代码如下:
public class Test04 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("day12\\a.txt");
int i = -1;
while ((i =fileInputStream.read()) != -1){
System.out.println((char)i);
}
fileInputStream.close();
}
}
题目5
- 利用字节输入流读取文件b.txt的内容,文件内容确定都为纯ASCII字符
,使用循环读取,一次读取一个字节数组,直到读取到文件末尾,将读取到的字节数组转换成字符串输出到控制台。
代码如下;
public class Test05 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("day12\\a.txt");
byte[] b = new byte[1024];
int len = 0;
while((len = fileInputStream.read(b))!= -1){
System.out.println(new String(b,0,len));;
}
fileInputStream.close();
}
}
题目6
- 利用字节流将E盘下的a.png图片复制到D盘下(文件名保存一致)
代码如下:
public class Test06 {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("c:\\Users\\wei\\Pictures\\成功失败法.png");
FileOutputStream fileOutputStream = new FileOutputStream("day12\\a.png");
int len = 0;
while((len = fileInputStream.read()) != -1){
fileOutputStream.write(len);
}
fileInputStream.close();
fileOutputStream.close();
}
}
题目7
- 请用户从控制台输入信息,程序将信息存储到文件Info.txt中。可以输入多条信息,每条信息存储一行。当用户输入:”886”时,程序结束。
代码如下:
public class Test07 {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
FileOutputStream fileOutputStream = new FileOutputStream("day12\\Info.txt",true);
String s;
while((s = scanner.next()).equals("886") == false){
fileOutputStream.write((s+"\r\n").getBytes());
fileOutputStream.flush();
}
fileOutputStream.close();
}
}
题目8
- 我有一个文本文件score.txt,我知道数据是键值对形式的,但是不知道内容是什么。请写一个程序判断是否有"lisi"这样的键存在,如果有就改变其实为"100"
-
score.txt文件内容如下:
zhangsan = 90 lisi = 80 wangwu = 85
代码如下:
public class Test08 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream("day12\\score.txt"));
Set<String> strings = properties.stringPropertyNames();
for (String key:strings
) {
if(key.equals("list")){
properties.setProperty("list","100");
}
}
properties.store(new FileOutputStream("day12\\score.txt"),"change");
}
}