import java.io.*; public class JavaExample14_1 { public static void main(String args[]) { try { FileOutputStream fout=new FileOutputStream(new File("d:/my.txt")); fout.write("2016122108 男 梁凡 计算机计 161 江苏苏州\r\n".getBytes()); fout.write("2016122109 女 高敏 计算机计 161 广东广州\r\n".getBytes()); fout.write("2016122201 男 张启帆 软件161 江苏南通".getBytes()); fout.close(); } catch(IOException e){ } try { FileInputStream fin=new FileInputStream(new File("d:/my.txt")); byte[]data=new byte[100]; int n; while((n=fin.read(data))!=-1) { String s=new String(data,0,n); System.out.println(s); } } catch(IOException ex){ } } }
时间: 2024-02-26 13:55:36 浏览: 21
这段代码实现了将一些字符串数据写入到文件中,然后再从文件中读取出来并输出到控制台的功能。
首先,在主函数中,程序尝试向文件"d:/my.txt"中写入一些字符串数据。为此,它使用了 FileOutputStream 类来打开文件,并使用 write() 方法将字符串转换为字节数组并写入到文件中。这个过程会写入三行数据,每行数据都包含了一些个人信息。
接着,程序使用 FileInputStream 类来读取这个文件中的数据。在执行这个操作时,程序将文件中的数据读入到一个 byte 数组中,并使用 read() 方法逐个字节地读取数据,直到文件结束。在这个过程中,程序使用了一个 while 循环,每次读取一个 byte 数组中的数据,并将其转换为字符串,然后输出到控制台中。
需要注意的是,程序在处理异常时使用了空的 catch 块,这是不推荐的做法。在实际开发中,应该对异常进行适当的处理,以提高程序的健壮性和可靠性。
相关问题
import java.util.Scanner; public class Test1 { 晋涛 public static void main(String[] args) { Sy
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的姓名:");
String name = scanner.nextLine();
.out.println("您好," + name + "!");
}
}
这段代码是一个简单的Java程序,它使用了Java标准库中的Scanner类来实现用户输入和输出。首先,我们通过创建Scanner对象来获取用户的输入。然后,使用nextLine()方法读取用户输入的姓名,并将其存储在name变量中。最后,通过System.out.println()方法将问候语和用户的姓名输出到控制台。
逐行分析解释下面代码:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) { try { Socke
这段代码是一个Java程序的客户端示例,用于与服务器进行通信。我们来逐行分析:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
```
这部分代码导入了Java中用于网络编程的库和输入输出类。
```
public class Client {
```
定义了一个名为Client的类。
```
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8080);
```
定义了一个main方法,程序从这里开始执行。try块中使用Socket类连接到一个服务器,Socket类的构造函数需要指定服务器的主机名和端口号。
```
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
```
创建了一个输入流和一个输出流,用于向服务器发送数据和接收数据。这里使用了Java的字符流,包装了底层的字节流。
```
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
```
创建了一个用户输入流,用于从控制台读取用户输入。
```
String userInputStr;
while ((userInputStr = userInput.readLine()) != null) {
out.println(userInputStr);
System.out.println("echo: " + in.readLine());
}
```
建立一个while循环,读取用户输入并向服务器发送数据。每次写入数据后,程序会等待服务器响应并将响应打印到控制台。
```
in.close();
out.close();
socket.close();
```
关闭了所有的流和Socket连接。
完整的代码如下:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8080);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String userInputStr;
while ((userInputStr = userInput.readLine()) != null) {
out.println(userInputStr);
System.out.println("echo: " + in.readLine());
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)