PrintWriter类的println()和DataOutputStream类中的writeint()
时间: 2024-02-26 19:55:58 浏览: 85
PrintWriter类的`println()`方法和DataOutputStream类中的`writeInt()`方法都可以将整数输出到文件中,但是它们的区别是:
1. `println()`方法会自动添加换行符,而`writeInt()`方法不会。
2. `println()`方法的输出类型不仅限于整数,还可以输出字符串、布尔值等任何类型,而`writeInt()`方法只能输出整数。
因此,如果你需要将多种类型的数据输出到文件中,并且需要在每个数据之间添加换行符,可以使用PrintWriter类的`println()`方法;如果你只需要输出整数,并且不需要添加换行符,可以使用DataOutputStream类的`writeInt()`方法。同时需要注意的是,使用`DataOutputStream`类进行输出时,还需要使用相应的`DataInputStream`类进行读取。
相关问题
解释这段代码package question7; import java.io.DataOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.Scanner; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.Random; public class Test { public static void main(String args[]){ Test test=new Test(); Scanner input=new Scanner(System.in); String s1=new String(); s1=input.nextLine(); String[] s2 = s1.split(" "); int[] l=new int[s2.length]; for(int i=0 ; i<s2.length;i++){ l[i]=Integer.parseInt(s2[i]); } test.write("test.txt", l); int[] readlist=test.read("test.txt",l); isPrime isprime=new isPrime(readlist); for(int i=1;i<=10;i++){ new Thread(isprime).start(); } try { Thread.sleep(1000); //1000 毫秒,也就是1秒. } catch(InterruptedException ex) { Thread.currentThread().interrupt(); } Set<Integer> set=new HashSet(); set=isprime.getSet(); System.out.println("素数set"+set); System.out.println("输入查询数字"); int num=input.nextInt(); if(set.contains(num)){ System.out.println(num+"是素数"); } else System.out.println(num+"不是是素数"); } public void write(String filename,int a[]){ File file = new File(filename); try { PrintWriter output=new PrintWriter(file); for(int i=0;i<a.length;i++){ output.print(a[i]+" "); } output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public int[] read(String filename,int a[]){ File file=new File(filename); try { Scanner input=new Scanner(file); int i=0; while(input.hasNext()){ a[i]=input.nextInt(); i++; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return a; } }
这段代码是一个 Java 程序,主要功能是读取用户输入的一串整数,将其写入文件 test.txt,然后又从这个文件中读取整数列表,并查找出其中的素数并存储在一个 HashSet 中,最后判断用户输入的数字是否为素数。具体实现包括定义了一个 isPrime 类用于判断一个数字是否为素数,使用了多线程来处理多个数字的判断,使用了 HashSet 来存储素数列表。
将Student对象(属性:int id, String name(不多于10个字符),int age,double grade)写入文件、读出文件,分别尝试使用以下方式实现: (1)DataInputStream, DataOutputStream; (2)Scanner(或BufferedReader), PrintWriter; (3)ObjectInputStream, ObjectOutputStream。 问题:1、2、3方法生成的文件大小各是多少?尝试分析1和2输出的文件大小。 要求:以方法的形式进行封装(使用方法重载),例如: public static void writeStudent(Student s, DataOutputStream dos){}; 或者:public static void writeStudent(List stus, DataOutputStream dos){}; 读取时类似: public static Student readStudent(DataInputStream dis){}; 或者:public static List readStudent(DataInputStream dis){};
以下是实现以上要求的代码:
```java
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Student {
private int id;
private String name;
private int age;
private double grade;
public Student(int id, String name, int age, double grade) {
this.id = id;
this.name = name;
this.age = age;
this.grade = grade;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getGrade() {
return grade;
}
public static void writeStudent(Student s, DataOutputStream dos) throws IOException {
dos.writeInt(s.getId());
dos.writeUTF(s.getName());
dos.writeInt(s.getAge());
dos.writeDouble(s.getGrade());
}
public static void writeStudent(List<Student> stus, DataOutputStream dos) throws IOException {
dos.writeInt(stus.size());
for (Student s : stus) {
writeStudent(s, dos);
}
}
public static Student readStudent(DataInputStream dis) throws IOException {
int id = dis.readInt();
String name = dis.readUTF();
int age = dis.readInt();
double grade = dis.readDouble();
return new Student(id, name, age, grade);
}
public static List<Student> readStudents(DataInputStream dis) throws IOException {
List<Student> stus = new ArrayList<>();
int num = dis.readInt();
for (int i = 0; i < num; i++) {
stus.add(readStudent(dis));
}
return stus;
}
public static void writeStudent(Student s, PrintWriter pw) {
pw.println(s.getId());
pw.println(s.getName());
pw.println(s.getAge());
pw.println(s.getGrade());
}
public static void writeStudent(List<Student> stus, PrintWriter pw) {
pw.println(stus.size());
for (Student s : stus) {
writeStudent(s, pw);
}
}
public static Student readStudent(Scanner scanner) {
int id = scanner.nextInt();
String name = scanner.next();
int age = scanner.nextInt();
double grade = scanner.nextDouble();
return new Student(id, name, age, grade);
}
public static List<Student> readStudents(Scanner scanner) {
List<Student> stus = new ArrayList<>();
int num = scanner.nextInt();
for (int i = 0; i < num; i++) {
stus.add(readStudent(scanner));
}
return stus;
}
public static void writeStudent(Student s, ObjectOutputStream oos) throws IOException {
oos.writeObject(s);
}
public static void writeStudent(List<Student> stus, ObjectOutputStream oos) throws IOException {
oos.writeObject(stus);
}
public static Student readStudent(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (Student) ois.readObject();
}
public static List<Student> readStudents(ObjectInputStream ois) throws IOException, ClassNotFoundException {
return (List<Student>) ois.readObject();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
List<Student> stus = new ArrayList<>();
stus.add(new Student(1, "Tom", 18, 90));
stus.add(new Student(2, "Jerry", 19, 80));
stus.add(new Student(3, "Mike", 20, 70));
// 使用DataInputStream, DataOutputStream
FileOutputStream fos1 = new FileOutputStream("data1.txt");
DataOutputStream dos1 = new DataOutputStream(fos1);
writeStudent(stus, dos1);
dos1.close();
FileInputStream fis1 = new FileInputStream("data1.txt");
DataInputStream dis1 = new DataInputStream(fis1);
List<Student> stus1 = readStudents(dis1);
dis1.close();
// 使用Scanner(或BufferedReader), PrintWriter
FileOutputStream fos2 = new FileOutputStream("data2.txt");
PrintWriter pw2 = new PrintWriter(fos2);
writeStudent(stus, pw2);
pw2.close();
FileInputStream fis2 = new FileInputStream("data2.txt");
Scanner scanner2 = new Scanner(fis2);
List<Student> stus2 = readStudents(scanner2);
scanner2.close();
// 使用ObjectInputStream, ObjectOutputStream
FileOutputStream fos3 = new FileOutputStream("data3.txt");
ObjectOutputStream oos3 = new ObjectOutputStream(fos3);
writeStudent(stus, oos3);
oos3.close();
FileInputStream fis3 = new FileInputStream("data3.txt");
ObjectInputStream ois3 = new ObjectInputStream(fis3);
List<Student> stus3 = readStudents(ois3);
ois3.close();
System.out.println("stus1: " + stus1);
System.out.println("stus2: " + stus2);
System.out.println("stus3: " + stus3);
File file1 = new File("data1.txt");
System.out.println("data1.txt size: " + file1.length() + " bytes");
File file2 = new File("data2.txt");
System.out.println("data2.txt size: " + file2.length() + " bytes");
File file3 = new File("data3.txt");
System.out.println("data3.txt size: " + file3.length() + " bytes");
}
}
```
上述代码中,使用了方法重载的形式,可以根据需要选择不同的方法进行读写操作。在main方法中,我们首先创建了一个包含三个学生的List对象stus,并分别使用DataInputStream、Scanner和ObjectInputStream进行写入文件操作,然后再使用相应的读取方式将文件中的数据读取出来并存入新的List对象中。最后,我们输出了三个List对象以及生成的三个文件的大小。需要注意的是,在使用DataInputStream和PrintWriter进行读写操作时,需要使用相应的流对象的close方法关闭流,而在使用ObjectInputStream和ObjectOutputStream进行读写操作时,不需要手动关闭流对象,因为它们会在读写完成后自动关闭。
阅读全文