「javaio题」java简答题
本篇文章给大家谈谈javaio题,以及java简答题对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
JavaIO操作题?
刚好有个以前写的练习记录,可能有参考作用://对象转字节数组publicstaticbyte[]GetBytes(Objectobj)throwsException{ ByteArrayOutputStreambos=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bos); oos.writeObject(obj); byte[]byteArray=bos.toByteArray(); returnbyteArray;}运行结果查看示例:提取码:m3pr
一道java io笔试题
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;
public class GiveMeMore {
private static final int counter = 1;
public static void main(String[] args) {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
System.out.print("请输入:");
String text = in.readLine();
char[] a = text.toCharArray();
MapCharacter, Integer m = new TreeMapCharacter, Integer();
for (int i = 0; i a.length; i++) {
if (!m.containsKey(a[i])) {
m.put(a[i], counter);
} else {
int num = m.get(a[i]);
m.put(a[i], num + 1);
}
}
System.out.println(m);
} catch (Exception e) {
e.printStackTrace();
}
}
}
java 实现写文件功能 io流题
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class User {
private String userName;
private String userPass;
public User(String userName, String userPass) {
super();
this.userName = userName;
this.userPass = userPass;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPass() {
return userPass;
}
public void setUserPass(String userPass) {
this.userPass = userPass;
}
public static void main(String[] args) throws IOException {
String userName = null;
String userPass = null;
ListUser userList = new ArrayListUser();
// Console console = System.console();
// for (int i = 0; i 5; i++) {
// userName = console.readLine("用户名:");
// userPass = console.readLine("密码:");
// User user = new User(userName, userPass);
// userList.add(user);
// }
Scanner console = new Scanner(System.in);
for (int i = 0; i 5; i++) {
System.out.print("用户名:");
userName = console.nextLine();
System.out.print("密码:");
userPass = console.nextLine();
User user = new User(userName, userPass);
userList.add(user);
}
console.close();
File file = new File("users.txt");
PrintWriter out = new PrintWriter(file, "UTF-8");
for (User user : userList) {
out.println(String.format("用户名: %s, 密码: %s", user.getUserName(), user.getUserPass()));
}
out.close();
}
}
用户名: 123, 密码: 333
用户名: 213, 密码: 333
用户名: asds, 密码: 123
用户名: sadas, 密码: 123
用户名: dasdas, 密码: 123
java题目,io流问题
我这里有一个简单的学生管理系统,你只需要把Student学生类修改成名片类就可以了。你需要新建立一个java文件名为HW4.java,复制粘贴以下代码,编译运行就可以了。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
class Student implements ComparableStudent, Serializable{
/**
* Serializable UID: ensures serialize/de-serialize process will be successful.
*/
private static final long serialVersionUID = -3515442620523776933L;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int number;
private int age;
private double weight;
private String name;
public Student(int number, int age, double weight, String name) {
this.number = number;
this.age = age;
this.weight = weight;
this.name = name;
}
@Override
public int compareTo(Student o) {
if (this.age == o.age) {
return (int)(this.weight - o.weight);
}
return this.age - o.age;
}
}
class StudentSortByAge implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge() - o2.getAge();
}
}
class StudentSortByWeight implements ComparatorStudent {
@Override
public int compare(Student o1, Student o2) {
return (int)(o1.getWeight() - o2.getWeight());
}
}
public class HW4 {
//passing one and only one instance of scanner instance reduce complexity of program.
public static void main(String[] args) {
System.out.println("\nWelcome to the System, Choose options below: ");
printPrompt();
Student[] students = null;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextInt()) {
switch (scanner.nextInt()) {
case 1:
System.out.println("Print Student Information");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
printStudents(students);
}
printPrompt();
break;
case 2:
System.out.println("Enter numebr of students you want to create: ");
int number = scanner.nextInt();
students = initilise(number, scanner);
printPrompt();
break;
case 3:
System.out.println("Add a new student");
printPrompt();
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
int newLength = students.length + 1;
students = Arrays.copyOf(students, newLength);
students[newLength - 1] = createStudent(scanner);
System.out.println("New student has been added.");
printPrompt();
}
break;
case 4:
System.out.println("Sorting by Age, Weight in Asce order");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
Student[] sortedStudents = students;
Arrays.sort(sortedStudents);
printStudents(sortedStudents);
}
break;
case 5:
System.out.println("Calcaulte Min, Max, Ave of Age and Weight");
if (students == null) {
System.out.println("Please Initilise N students first");
} else {
Student[] sortedAgeStudents = students;
Arrays.sort(sortedAgeStudents, new StudentSortByAge());
Student[] sortedWeightStudents = students;
Arrays.sort(sortedWeightStudents, new StudentSortByWeight());
int averageAge = 0;
double averageWeight = 0.0;
for (Student student : sortedAgeStudents) {
averageAge += student.getAge();
averageWeight += student.getWeight();
}
averageAge = averageAge / sortedAgeStudents.length;
averageWeight = averageWeight / sortedWeightStudents.length;
System.out.printf("Min Age: %d, Max Age: %d, Ave Age: %d\n", sortedAgeStudents[0].getAge(), sortedAgeStudents[sortedAgeStudents.length - 1].getAge(), averageAge);
System.out.printf("Min Weight: %f, Max Weight: %f, Ave Weight: %f\n", sortedWeightStudents[0].getWeight(), sortedWeightStudents[sortedWeightStudents.length - 1].getWeight(), averageWeight);
}
break;
case 6:
System.out.println("Write Student data into file");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("studentsData"), true))) {
oos.writeObject(students);
printPrompt();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 7:
System.out.println("Read studentData from file");
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("studentsData")))) {
students = (Student[]) (ois.readObject());
printPrompt();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
scanner.close();
System.out.println("Quit");
break;
}
}
}
public static void printPrompt() {
System.out.println("1: Display current students");
System.out.println("2: Initilise N students");
System.out.println("3: Add new student");
System.out.println("4: Sorting by Age, Weight in Asce order");
System.out.println("5: Calcaulte Min, Max, Ave of Age and Weight");
System.out.println("6: Write Student data into file");
System.out.println("7: Read studentData from file");
}
public static Student[] initilise(int n, Scanner scanner) {
Student[] students = new Student[n];
for (int i = 0; i n; i ++) {
students[i] = createStudent(scanner);
}
System.out.println("Initilisation of students complete.");
return students;
}
public static void printStudents(Student[] students) {
for (Student student : students) {
System.out.printf("Student: %s, Number: %d, Age: %d, Weight: %f\n", student.getName(), student.getNumber(), student.getAge(), student.getWeight());
}
}
public static void printSortedStudents(Student[] students) {
for (Student student : students) {
System.out.printf("Age: %d, Weight: %f, Student: %s, Number: %d\n", student.getAge(), student.getWeight(), student.getName(), student.getNumber());
}
}
public static Student createStudent(Scanner scanner) {
int studentNumber = 0, studentAge = 0;
double studentWeight = 0.0;
String studentName = null;
System.out.println("Enter Student Number: ");
while(scanner.hasNext()) {
studentNumber = scanner.nextInt();
System.out.println("Enter Student Age: ");
studentAge = scanner.nextInt();
System.out.println("Enter Student Weight: ");
//nextDouble仅仅读取double的值,在double值后的'\n'将会被nextLine()所读取,所以读取studentName时需要再读取一次nextLine()
studentWeight = scanner.nextDouble();
System.out.println("Enter Student Name: ");
scanner.nextLine();
studentName = scanner.nextLine();
break;
}
return new Student(studentNumber, studentAge, studentWeight, studentName);
}
}
关于javaio题和java简答题的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-18,除非注明,否则均为
原创文章,转载请注明出处。