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 12:55:34 浏览: 29
这段代码是一个 Java 程序,主要的功能是将一些文本数据写入到文件中,然后再从文件中读取出来并输出到控制台。
具体来说,程序通过创建一个 FileOutputStream 对象实例来打开一个名为"d:/my.txt"的文件,然后使用 write() 方法将一些字符串数据写入到文件中。接着,通过创建一个 FileInputStream 对象实例来读取文件中的数据,并使用 read() 方法将数据读取到一个 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();
}
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.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)