[ Java Programming ] Reading and Writing text files tutorial

Published: 04 April 2018
on channel: Code Splosion
117
1

I hope you were able to make use of this video. Included below is the source code that was utilized in this project.

package javaio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**

@author Code Splosion :)
*
*/
public class FileInputOutput {
public static void main(String[] args) {
String stringOfInfo = "Hello World. Today's date is 04 April, 2018";
String result = "";
try {
System.out.println("Writing to file");
writeFile(stringOfInfo, "textinfo.txt");
System.out.println();
result = readData("textinfo.txt");

} catch (FileNotFoundException exceptio) {
exceptio.printStackTrace();
}

System.out.println();
System.out.println(result);
}

private static String readData(String fileName) throws FileNotFoundException {
File fileObj = new File(fileName);
Scanner scanner = new Scanner(fileObj);
String result = "";

while(scanner.hasNextLine()) {
result += scanner.nextLine();
}

scanner.close();

return result;
}

private static void writeFile(String stringOfInfo, String fileName) throws FileNotFoundException {
File fileObj = new File(fileName);
PrintWriter pw = new PrintWriter(fileObj);

pw.println(stringOfInfo);

pw.close();

}
}


On this page of the site you can watch the video online [ Java Programming ] Reading and Writing text files tutorial with a duration of hours minute second in good quality, which was uploaded by the user Code Splosion 04 April 2018, share the link with friends and acquaintances, this video has already been watched 117 times on youtube and it was liked by 1 viewers. Enjoy your viewing!