实验目的:
-
掌握Java 输入输出流的应用
-
掌握缓冲流的应用。
实验要求:
(1)掌握字节输入输出流的操作
(2)掌握字符输入输出流的操作
(3)理解字节流和字符流的区别
(4)代码应遵循Java编程规范,包含恰当的注释说明。
实验内容:
1.附件中有一张图片,文件名为1.jpg,编写一个程序,通过main方法传递源图片地址和目标地址,实现图片的复制。例如:类名为Test,通过调用java Test d:\test\1.jpg d:\test\2.jpg,完成将d盘test目录下的1.jpg图片复制为d盘test目录下的2.jpg图片。
提示:图片文件的内容是二进制代码表示时,通过字节流实现图片的复制。
2.附件中有一个文本文件demo1.txt,编写程序,统计文件中指定的单词出现次数,并在屏幕上输入结果。
例如:类名为Test,通过调用java Test d:\test\demo1.txt Trump,
屏幕输出:Trump出现次数:3。
3.题目需求:完成文件复制操作
请将附件中的demo1.txt文件拷贝到D:\test目录下。通过程序完成将demo1.txt文件内容复制到一个新的文件demo2.txt(注意:新文件在程序中使用createNewFile方法创建),并将其中的所有小写字母转换为大写字母,大写字母转换为小写字母。
demo1.txt 内容如下:
Election 2020 live updates and resultsLast Updated: November 5, 2020, 10:32 AM ETElection Day is turning into election week as the counting of votes continues across the United States, with preliminary results showing tight races in several key battleground states.Candidates need 270 Electoral College votes to secure the presidency. ABC News projects President Donald Trump currently has 214, while former Vice President Joe Biden has 253. Biden's count includes Wisconsin, where ABC News is characterizing him as the apparent winner because the vote is very close and has not yet been certified.MORE: Election 2020 undecided races: The presidency, key states and Senate contests Both Trump, the Republican incumbent, and Biden, the Democratic challenger, addressed the nation in the early hours of Wednesday morning, each expressing confidence in the race for the White House. By Wednesday evening, Biden again delivered remarks urging patience, but Trump remained out of public view. The president took to Twitter instead to spout baseless claims of voter fraud and falsely call races for himself, while his lawyers waged legal battles to halt vote counting in crucial states.
实验1源代码:
package Package240529;
import java.io.*;
public class Test1 {
public static void main(String[] args) throws Exception{
// 定义源文件路径和目标文件路径
String path1="d:"+File.separator+"test"+File.separator+"1.jpg";
String path2="d:"+File.separator+"test"+File.separator+"2.jpg";
// 创建文件对象
File file1=new File(path1);
File file2=new File(path2);
// 创建输入流,读取源文件内容
InputStream input=new FileInputStream(file1);
byte[] b=new byte[(int)file1.length()];
input.read(b);
// 创建输出流,将读取到的内容写入目标文件
OutputStream output=new FileOutputStream(path2);
output.write(b);
// 关闭输入输出流
output.close();
input.close();
}
}
实验2源代码:
package Package240529;
import java.io.*;
public class Test2 {
public static void main(String[] args) throws Exception {
// 定义文件路径和要查找的单词
String filePath = "d:" + File.separator + "test" + File.separator + "demo1.txt";
String word = "Trump";
// 创建文件对象
File file = new File(filePath);
// 创建输入流,读取文件内容
InputStream input = new FileInputStream(file);
// 创建一个字节数组,用于存储文件内容
byte[] b = new byte[(int) file.length()];
// 将文件内容读取到字节数组中
input.read(b);
// 关闭输入流
input.close();
// 将字节数组转换为字符串
String content = new String(b);
// 初始化计数器
int count = 0;
// 查找单词在字符串中的位置
int index = content.indexOf(word);
// 循环查找单词出现的次数
while (index != -1) {
count++;
index = content.indexOf(word, index + 1);
}
// 输出单词出现的次数
System.out.println(word + "出现次数:" + count);
}
}
实验3源代码:
package Package240529;
import java.io.*;
public class Test3 {
public static void main(String[] args) throws Exception {
// 定义源文件路径和目标文件路径
String path1 = "d:" + File.separator + "test" + File.separator + "demo1.txt";
File file1 = new File(path1);
String path2 = "d:" + File.separator + "test" + File.separator + "demo2.txt";
File file2 = new File(path2);
// 如果目标文件不存在,则创建新文件
if (!file2.exists()) {
file2.createNewFile();
}
// 创建输入流,读取源文件内容
InputStream input = new FileInputStream(file1);
byte[] b = new byte[(int) file1.length()];
input.read(b);
// 创建输出流,将处理后的内容写入目标文件
OutputStream output = new FileOutputStream(file2);
for (int i = 0; i < file1.length(); i++) {
// 判断字符是否为大写字母,如果是则转换为小写字母
if (b[i] >= 65 && b[i] <= 90) {
b[i] += 32;
} else if (b[i] >= 97 && b[i] <= 122) { // 判断字符是否为小写字母,如果是则转换为大写字母
b[i] -= 32;
}
// 将处理后的字符写入目标文件
output.write(b, i, 1);
}
// 关闭输入输出流
input.close();
output.close();
}
}