InputStreamReader 和Output Stream Writer类的作用
时间: 2023-10-25 10:38:18 浏览: 84
InputStreamReader 和 OutputStreamWriter 类是 Java IO 包中的类,它们的作用是将字节流转换为字符流,或者将字符流转换为字节流。这两个类主要用于处理字符编码和字符集的问题。
InputStreamReader 类将字节流转换为字符流。它读取字节并使用指定的字符集将其转换为字符。它还提供了一些方法来读取字符流,如 read() 和 readLine() 方法。
OutputStreamWriter 类将字符流转换为字节流。它接受字符流并使用指定的字符集将其转换为字节。它还提供了一些方法来写入字符流,如 write() 方法。
这两个类通常与其他 IO 类一起使用,如 FileInputStream 和 FileOutputStream,以实现对文件的读写操作。它们也可以用于网络编程中,例如将网络流转换为字符流或将字符流转换为网络流。
相关问题
In this exercise you will create a program to read text from standard input and write it to a file with each line prefixed with a line-number count. This file will be specified by a command-line argument. Directions Create a program called TestLineNumbering that accepts one command-line argument, the name of a file. Your code should verify that the command-line argument was specified. Declare a variable that holds a File object that is initialized from the file named in the first command-line argument. Within a try-catch block, create a buffered, input stream reader based on the System.in stream (standard input). Also, create a print file writer based on the File variable. Write the while loop that reads each line from the console input stream and prints the line out to the file. Each line should be prepended with the count of the line number. Make sure to close the I/O streams.
Here is a possible solution in Java:
```
import java.io.*;
public class TestLineNumbering {
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java TestLineNumbering <output-file>");
System.exit(1);
}
String fileName = args[0];
try {
File outputFile = new File(fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(outputFile);
String line;
int lineNumber = 0;
while ((line = reader.readLine()) != null) {
writer.printf("%d: %s%n", ++lineNumber, line);
}
writer.close();
reader.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
System.exit(2);
}
}
}
```
The program checks that exactly one command-line argument is provided, which is the name of the output file. It then creates a `BufferedReader` to read from standard input, and a `PrintWriter` to write to the output file. The program reads each line from standard input, increments a line number counter, and writes the line with the line number prefix to the output file. Finally, the I/O streams are closed, and any exceptions are caught and reported.
java编写程序,使用URL类的openStream()方法实现读取URL数据并存储在本地文件中
在 Java 中,可以使用 `URL` 类的 `openStream()` 方法来读取 URL 中的数据,并将其存储在本地文件中。
例如,下面的代码演示了如何从 URL 读取数据并将其存储在本地文件 "output.txt" 中:
```
import java.io.*;
import java.net.URL;
public class Main {
public static void main(String[] args) throws IOException {
// 创建 URL 对象
URL url = new URL("http://www.example.com/data.txt");
// 打开 URL 并读取数据
InputStream inputStream = url.openStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
// 创建输出流并将数据写入本地文件
FileOutputStream outputStream = new FileOutputStream("output.txt");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
// 关闭流
reader.close();
inputStream.close();
writer.close();
outputStream.close();
}
}
```
在上面的代码中,我们使用 `URL` 对象的 `openStream()` 方法打开了 URL 并读取了其数据。然后,我们使用 `FileOutputStream` 和 `BufferedWriter` 创建了输出流,并将数据写入本地文件 "output.txt" 中。
最后,我们关闭了所有流,以确保在程序结束时释放资源。
阅读全文