0% found this document useful (0 votes)
4 views

Lab File IO

The document outlines various Java programming exercises focused on file and input/output operations. It includes programs for reading from and writing to files, handling console input, and using byte and character streams. Each section provides code snippets and instructions for implementing the exercises.

Uploaded by

Tesfalegn Yakob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab File IO

The document outlines various Java programming exercises focused on file and input/output operations. It includes programs for reading from and writing to files, handling console input, and using byte and character streams. Each section provides code snippets and instructions for implementing the exercises.

Uploaded by

Tesfalegn Yakob
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Debre Markos University

Bure Compus
Department of Computer Science
Java Programming
File and IO Lab Exercise

Reading Console Input


Reading and Writing Files Using Byte Streams
1. Write a java program which reads a file from the user 3. A java program that reads a file from a disk i.e
and display the result on the screen? reading text file(s) and display it on the screen

package WritingFIO;
package FileReadFromUser; import java.io.*;
import java.io.*;
public class WritingFIO {
public class FileReadFromUser {
public static void main(String[] args) throws IOException{
public static void main(String[] args) throws int i;
IOException{ try{
FileInputStream fin = new
byte[] b = new byte[10]; FileInputStream("in.txt");
do{
System.out.println("Enter some characters:");
i=fin.read();
System.in.read(b); System.out.print(i);
try{ }while(i!=-1);
for(int i=0;i<b.length;i++) System.out.println();
System.out.println((char)b[i]); }catch(IOException e){
}catch(Exception e){ System.out.println("File not
System.out.println(e.getMessage()); found."+e.getMessage());
} }
} }
}
}

Writing Console Output 4. A java program that accepts characters from the
user and write it on the disk e.g to outputFile2.txt
2. A java program that write a character on the console
program
package ReadWriteFile;
import java.io.*;
package FileWriteConsoleOutput;
import java.io.*;
public class ReadWriteFile {
public class FileWriteConsoleOutput {
public static void main(String[] args) throws
IOException{
public static void main(String[] args) {
byte[] b = new byte[100];
System.out.println("Enter some numbers: ");
char mychar='a';
System.in.read(b);
System.out.write(mychar);
try{
System.out.println("\n");
FileOutputStream fout=new
}
FileOutputStream("outputFile2.txt");
fout.write(b);
}
}catch(IOException ex){
System.out.println(ex.getMessage());
}
}
}
5. A java program that reads a file from disk (e.g. Console Output Using Character Streams
myfile1.txt) and write to disk (e.g. myfile2.txt)
package ReadWriteFile; 7. A Java Console Output Using Character Streams
import java.io.*; from users.
package WritingCharacters;
public class ReadWriteFile { import java.io.*;

public static void main(String[] args) throws public class WritingCharacters {


IOException{
int i; public static void main(String[] args) throws
FileInputStream fin = new IOException{
FileInputStream("myfile1.txt");
FileOutputStream fout = new try{
FileOutputStream("myfile2.txt"); PrintWriter pw = new PrintWriter(System.out,
true);
try{ int a=0;
do{ double b = 23.54;
i=fin.read(); pw.println(a);
if(i!=-1) pw.println(b);
fout.write(i); pw.println(a + " + " + b + " = " + (a+b));
}while(i!=-1); }catch(Exception e){
}catch(IOException ex){ System.out.println(e.getMessage());
System.out.println(ex.getMessage()); }
} }
} }
}

Console Input Using Character Streams Reading


Characters

6. A java program that reads strings/characters from


console and display on screen
package ReadCharacters;
import java.io.*;

public class ReadCharacters {

public static void main(String[] args) throws


IOException{

String d;
try{
BufferedReader bf = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter some character, (.) to
exit");
do{
d =bf.readLine();
System.out.println(d);
}while(d!=".");

}catch(IOException e){
System.out.println(e.getMessage());
}
}
}

You might also like