Programming Fundamentals Lab Manual 12
Programming Fundamentals Lab Manual 12
Statement Purpose:
The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.
Activity Outcomes:
Student will be able to:
- Describe the concept of an I/O stream
- Explain the difference between text files and binary files
- Save data
- Read data
1) Stage J (Journey)
2) Stage a1 (apply)
Lab Activities:
Activity 1:
Write a java program to get a files size in bytes?
Solution:
import java.io.File; public class Main { public static long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File doesn\'t exist"); return -1; } return file.length(); } public static void main(String[] args) { long size = getFileSize("c:/java.txt"); System.out.println("Filesize in bytes: " + size); } }
Activity 2:
Write a java program to get a files size in bytes?
Solution:
import java.io.File; public class Main { public static long getFileSize(String filename) { File file = new File(filename); if (!file.exists() || !file.isFile()) { System.out.println("File doesn\'t exist"); return -1; } return file.length(); } public static void main(String[] args) { long size = getFileSize("c:/java.txt"); System.out.println("Filesize in bytes: " + size); } }
Activity 3:
Write a java program to change the last modification time of a file?
Solution:
import java.io.File; import java.util.Date; public class Main { public static void main(String[] args) throws Exception { File fileToChange = new File ("C:/myjavafile.txt"); fileToChange.createNewFile(); Date filetime = new Date (fileToChange.lastModified()); System.out.println(filetime.toString()); System.out.println (fileToChange.setLastModified (System.currentTimeMillis())); filetime = new Date (fileToChange.lastModified()); System.out.println(filetime.toString()); } }
Activity 4:
Write a java program to create a temporary file?
Solution:
import java.io.*; public class Main { public static void main(String[] args) throws Exception { File temp = File.createTempFile ("pattern", ".suffix"); temp.deleteOnExit(); BufferedWriter out = new BufferedWriter (new FileWriter(temp)); out.write("aString"); System.out.println("temporary file created:"); out.close(); } }
Activity 5:
Write a java program to append a string in an existing file?
Solution:
import java.io.*;
public class Main
{ public static void main(String[] args) throws Exception
{ try
{
BufferedWriter out = new BufferedWriter(new FileWriter("filename"));
out.write("aString1\n"); out.close();
out = new BufferedWriter(new FileWriter("filename",true));
out.write("aString2"); out.close();
BufferedReader in = new BufferedReader(new FileReader("filename"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
} } in.close(); catch (IOException e) {
System.out.println("exception occoured"+ e);
}
}
}
3) Stage v (verify)
Home Activities:
Activity 1: Write a java program to copy one file into another file?
4) Stage a2 (assess)
Assignment:
- Write a java program to read a file?
- Write a java program to write into a file?
Comments
Post a Comment