/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package lab12;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* @author For Students
*/
public class Lab12 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
java.io.File file = new java.io.File("C:\\Users\\For Students\\Desktop\\act3.PNG");
System.out.println("Does it exist? " + file.exists());
System.out.println("The file has " + file.length() + " bytes ");
System.out.println("Can it be read? " + file.canRead());
System.out.println("Can it be written? " + file.canWrite());
System.out.println("Is it a directory? " + file.isDirectory());
System.out.println("Is it a file? " + file.isFile());
System.out.println("Is it absolute? " + file.isAbsolute());
System.out.println("Is it hidden? " + file.isHidden());
System.out.println("Absolute path is " +
file.getAbsolutePath());
System.out.println("Last modified on " +new java.util.Date(file.lastModified()));
System.out.println("---------------Write Data------------");
writeData();
System.out.println("---------------Read Data------------");
readData();
}
public static void writeData() throws FileNotFoundException
{
java.io.File file = new java.io.File("C:\\Users\\For Students\\Desktop\\scores.txt");
if (file.exists()) {
System.out.println("File already exists");
}
java.io.PrintWriter output = new java.io.PrintWriter(file);
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
output.close();
}
public static void readData() throws FileNotFoundException
{
java.io.File file = new java.io.File("C:\\Users\\For Students\\Desktop\\scores.txt");
Scanner input = new Scanner(file);
while (input.hasNext()) {
String firstName = input.next();
String mi = input.next();
String lastName = input.next();
int score = input.nextInt();
System.out.println(
firstName + " " + mi + " " + lastName + " " + score);
}
input.close();
}
}