Create a file that has some college department names and enr
Create a file that has some college department names and enrollments. For example, it might look like this:
Aerospace 201
Mechanical 66
Write a script that will read the information from this file and create a new file that has just the first four characters from the department names, followed by the enrollments. The new file will be in this form:
Aero 201
Mech 66
Solution
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String args[]) throws IOException
{
BufferedReader br = readFile();
BufferedWriter bw = writeFile();
String line = null;
while( (line = br.readLine())!= null ) {
String [] tokens = line.split(\" \");
bw.write(tokens[0].substring(0, 4) + \" \" + tokens[1]);
bw.newLine();
}
br.close();
bw.close();
}
public static BufferedReader readFile() throws FileNotFoundException
{
File file = new File(\"collegeIn\");
return new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
public static BufferedWriter writeFile() throws FileNotFoundException
{
File file = new File(\"collegeOut\");
return new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
}
}

