public boolean readin(String file) { try { Scanner fileScan = new Scanner(new File(file)); String s = null; String[] student = new String[5]; while (fileScan.hasNextLine()) { s = fileScan.nextLine(); student = s.split(";"); studentList.add(student); } fileScan.close(); fileScan=null; } catch (Exception except) { System.err.println(except); System.out.println("The Error message goes here"); } // System.out.println("共读取学生数据 : " + studentList.size()); return true; }
时间: 2024-03-29 12:35:48 浏览: 23
这段代码是一个方法,它的作用是从指定的文件中读取学生信息,并将这些信息存储在studentList中。具体实现是通过Scanner类读取文件,然后使用split方法将每一行学生信息分割成一个String数组,该数组包含了学生的各个属性,例如姓名、学号、专业、费用等。然后将这个数组添加到studentList中。在try块的末尾,将Scanner对象关闭,并将其引用置为null。在catch块中,如果发生异常,将异常信息打印到标准错误输出流,然后返回true。最后,这个方法返回一个布尔值,表示读取文件是否成功。如果成功读取文件并将学生信息存储到studentList中,方法返回true,否则返回false。
相关问题
public static Map<String, Boolean> SG = new HashMap<>();
As an AI language model, I don't have any context or information about what this code is trying to accomplish. However, I can explain what the code is doing in general.
This line of code initializes a new HashMap called "SG" with a key type of String and a value type of Boolean. HashMap is a data structure in Java that allows for efficient key-value mapping and retrieval. The purpose of this specific HashMap is not clear without any additional context.
Scanner sc = new Scanner(System.in);怎么使用?
Scanner类是Java中的一个输入类,用于从控制台或文件中读取输入。使用Scanner类需要先导入java.util包。以下是Scanner类的使用示例:
1. 从控制台读取字符串:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符串: ");
String str = sc.nextLine();
System.out.println("输入的字符串是: " + str);
```
2. 从控制台读取整数:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入整数: ");
int num = sc.nextInt();
System.out.println("输入的整数是: " + num);
```
3. 从控制台读取浮点数:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入浮点数: ");
double num = sc.nextDouble();
System.out.println("输入的浮点数是: " + num);
```
4. 从控制台读取布尔值:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入布尔值(true/false): ");
boolean b = sc.nextBoolean();
System.out.println("输入的布尔值是: " + b);
```
5. 从控制台读取字符:
```java
Scanner sc = new Scanner(System.in);
System.out.print("请输入字符: ");
char c = sc.next().charAt(0);
System.out.println("输入的字符是: " + c);
```
以上是Scanner类的基本使用方法,可以根据需求进行适当的变化。