Import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Program {
public static void main(String[] args) {
File fileIn = new File("C:\\Users\\LENOVO\\Desktop\\cool.t…
File fileOut = new File("C:\\Users\\LENOVO\\Desktop\\JACK.t…
Service service = new Service();
try {
Student student = service.read(fileIn);
service.save(fileOut, student);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class Service {
public Student read(File source) throws FileNotFoundException {
Student student = null;
Scanner file = new Scanner( source);
if (file.hasNext(. {
String line = file.nextLine();
String action = line.split("\\s")[0];
if ("ADDSTUDENT".equalsIgnoreCase( action. {
int pos = line.indexOf(' ');
student = new Student( line.substring(pos + 1. ;
}
}
file.close();
return student;
}
public void save(File file, Student student) throws IOException {
if (!file.exists(. {
file.createNewFile();
}
FileOutputStream fos = new FileOutputStream( file);
byte[] data = student.toString( ).getBytes();
fos.write(data);
fos.flush();
fos.close();
}
}
class Student {
private int id;
private String firstName;
private String lastName;
public Student() {
}
public Student(String value) {
String[] fields = value.split("\\s");
id = Integer.parseInt(fields[0]);
firstName = fields[1];
lastName = fields[2];
}
@Override
public String toString() {
return String.format("Id:%d FirstName:%s LastName:%s", id, firstName,
lastName);
}
}