import java.io.*;public class CopyFileStream{public static void main(String[] args) throws IOException{ //创建文件输出流sourceout,建立源文件FileOutputStream sourceout = new FileOutputStream("source.txt");sourceout.write('H');sourceout.write(69);sourceout.write(76);sourceout.write('L');sourceout.write('O');sourceout.write('!');// 关闭数据流sourceout.close();FileInputStream sourcein = new FileInputStream("source.txt");FileOutputStream targeout = new FileOutputStream("targe.txt");// 实现文件复制while(sourcein.available() > 0)targeout.write((byte)sourcein.read());// 关闭数据流sourcein.close();targeout.close();}}修改程序CopyFileStream.java,要求 利用main方法的参数,输入得到输入流和输 出流文件名。
时间: 2024-02-26 22:53:01 浏览: 21
import java.io.*;
public class CopyFileStream {
public static void main(String[] args) throws IOException {
// 从命令行参数中获取输入输出流文件名
String sourceFileName = args[0];
String targetFileName = args[1];
// 创建文件输入流sourcein,建立源文件
FileInputStream sourcein = new FileInputStream(sourceFileName);
// 创建文件输出流targeout,建立目标文件
FileOutputStream targeout = new FileOutputStream(targetFileName);
// 实现文件复制
while (sourcein.available() > 0) {
targeout.write((byte) sourcein.read());
}
// 关闭数据流
sourcein.close();
targeout.close();
}
}
相关问题
import java.io.*; import java.util.Arrays; import java.util.Scanner; public class FileTest { public static void main(String[] args) throws IOException { // 接收给定的一行字符串 Scanner scanner = new Scanner(System.in); String line = scanner.nextLine();
// 创建文件对象,并检查文件是否存在 File file = new File(line); if (!file.exists()) { System.out.println("文件不存在!"); return; } // 读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(file)); String content = ""; String temp; while ((temp = reader.readLine()) != null) { content += temp + "\n"; } // 关闭文件读取器 reader.close(); // 输出文件内容 System.out.println("文件内容:\n" + content); } }
这段代码是一个读取文件并输出文件内容的Java程序。首先,通过Scanner接收用户输入的文件路径,然后判断文件是否存在。如果文件不存在,则输出提示信息并结束程序。如果文件存在,则创建文件读取器并读取文件内容,将读取到的内容存储在一个字符串变量中。最后,关闭文件读取器并输出文件内容。
package step1; import java.io.*; public class TestInput { public static void main(String[] args) throws IOException { // 读取src/step1/input.txt文件的内容,并输出到控制台 /********* Begin *********/ /********* End *********/ } }
您好!根据您提供的代码,您想要读取指定文件的内容并将其输出到控制台。可以使用Java中的FileInputStream和BufferedReader来实现:
```java
package step1;
import java.io.*;
public class TestInput {
public static void main(String[] args) throws IOException {
// 定义文件路径
String filePath = "src/step1/input.txt";
// 创建文件输入流
FileInputStream fileInputStream = new FileInputStream(filePath);
// 创建缓冲读取器
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 读取文件内容并输出到控制台
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
// 关闭流
bufferedReader.close();
fileInputStream.close();
}
}
```
代码中,定义了文件路径,创建了文件输入流和缓冲读取器,然后使用while循环读取文件内容并输出到控制台。最后要记得关闭流。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)