Write a Java Program, that opens the file "students.txt"
1:mohamed:ali:0504123456:cs102:cs202
Show your results and provide your code.
txt file contain this
1:mohamed:ali:0504123456:cs102:cs202:cs330
2:ahmed:hamdi:0507415985:cs460:cs320
3:kamel:salem:0503245714:cs140
4:Mohsen:Mohamed:0503245714:cs140:cs150
simple code
Note the directory structure. I have a
Project
|->src
|->FileManupulation.java
|->students.txt
The java file and the text file are in same dierctory
FileManpulation.Java
package file.manupulation;
import java.io.IOException; //contains exceptions the input
output exception handling
import java.nio.file.Files; //java.nio.file is the API to handle
input output operation.It was included in JAVA 7
import java.nio.file.Path; //Contains operations for file
paths
import java.nio.file.Paths; //Contains operation for file
paths
import java.util.ArrayList; //to use the ArrayList
import java.util.List; // to use the List interface
class Student{ //student class stores individual student
information
public String id;
public String firstName;
public String lastName;
public String mobile;
public List<String> courses;
public Student(String id,String firstName,String
lastName,String mobile) { //the constructor. We don't add the
courses through it
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.mobile = mobile;
this.courses = new
ArrayList<String>();
}
}
public class FileManupulation {
public static void main(String[] args) throws
IOException {
Path pathOfFile =
Paths.get("./src/students.txt");
//"./src/students.txt" is relative path to the
students.txt file. You can mention the absolute path instead.
List<String> lines =
Files.readAllLines(pathOfFile);
//stores all the content of the file as list of strings
List<Student> students = new
ArrayList<Student>();
//List of all the students
List<String> studentPhoneList
= new ArrayList<String>(); //this the list for
storing the phone numbers
for(String details:lines) {
String[]
components = details.split(":");
//we take out different components of the
students using ":" as delimiter
String firstName
= components[1].substring(0, 1).toUpperCase() +
components[1].substring(1); //to make the first
name start with upper case
String lastName
= components[2].substring(0, 1).toUpperCase() +
components[2].substring(1); //to
make the last name start with upper case
Student student
= new
Student(components[0],firstName,lastName,components[3]);
//stores id,first name,last name,mobile
studentPhoneList.add(components[3]);
//store
the phone number in studentPhoneList
students.add(student);
for(int
i=4;i<components.length;i++)
//adding
the courses
student.courses.add(components[i]);
}
/*printing all the student
details*/
for(Student student:students)
{
System.out.println(">Id = "+ student.id + "\n>First Name = "
+ student.firstName + "\n>Last Name = " + student.lastName +
"\n>Mobile = " + student.mobile );
System.out.print(">Courses = " + student.courses.get(0));
for(int
i=1;i<student.courses.size();i++)
System.out.print(","+
student.courses.get(i));
System.out.println("\n");
}
}
}
Refer to the below images for proper indentaion and output
Get Answers For Free
Most questions answered within 1 hours.