Scanner and PrintWriter in Java – read from and write into file

First create in Netbeans IDE project of ScannerAppFile name. In last post I describe how create project in Java. In next step begin with create two file: for_read.txt and for_write.txt. How and where create them? right click on scannerappfile node in your project. From list select New and Other. q3 From Categories choose Other and from File Types choose Empty File. q4 Click on Next button. In File Name field write for_read.txt and click on Finish button. q5 In the same way create for_write.txt file in this package. It is the same package, which have main class. q6 Open file for_read.txt and write in it lines with numbers: 46 56 78 128 123 345 45 6 q16 In this file must be no empty line. Open for_write.txt file. It is empty file. q9 In main class paste this code:

package scannerappfile;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Scanner;

public class ScannerAppFile
{
public static void main(String[] args) throws Exception {
try {
Scanner in = new Scanner(Paths.get(System.getProperty("user.dir")+"\\src\\scannerappfile\\for_read.txt"));
PrintWriter out = new PrintWriter( System.getProperty("user.dir")+"\\src\\scannerappfile\\for_write.txt");
while(in.hasNextLine()) {
String s = in.nextLine();
Scanner inin = new Scanner (s);
int number1 = inin.nextInt();
int number2 = inin.nextInt();

inin.close();
int result = number1+number2;
out.println(""+result);
}
out.close();

in.close();
} catch ( Exception e) { System.out.println("Exception"+e.getMessage() ); }
}
}

 

jj1

You create Scanner object with constructor with path to the file. Scanner get path from Paths class and its get method. Current path to the netbeans project we obtain from getProperty method System class, passing user.dir parameter. Further part this path is path in project to the files, so it is src/scannerappfile folder. Scanner is used to read file and for write to the file is used PrintWriter class. To constructor PrintWriter class you may pass path to the file in the same way as in Scanner object. In next line implement while loop. Call hasNextLine method for Scanner object. It checks if is next line in this file. If yes, the another Scanner object is created for this line. String with this line is pass as parameter to Scanner object. From this new Scanner object call nextInt method twice for two numbers. To result variable is assigned sum of these numbers. You must either close stream this Scanner object. Call println method PrintWriter object, assigned result to for_write.txt file. In this file You see results of added numbers in all lines from for_read.txt file. After loop remember close out of PrintWriter object and outer Scanner object. You must remember also about import all packages for needed classes. Run your application. Remember that You have for_write.txt file empty. q14 After running project, you see this file filling results added numbers. q15   If You have empty lines in for_read.txt,   q17   You see this error: q18and for_write.txt fle will be empty. In this situation only need to delete empty lines in this file and application go on.