This example uses BYTES Streams " FileInputStream",
- copy contents of one file to another using BYTE streams (8 bytes
attributes, comparing with Character Streams 16 Bytes).
- BYTE streams should be used with primitive data types, only.
- use of finally
|
|
package ch4_package;
import java.io.*;
public class Test_This {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String file_name ="", str_text="";
Process pp = new Process();
try{
//System.out.println("---While Loop---");
System.out.print("Please Enter a File name: ");
file_name = br.readLine();
System.out.print("Now the text to be added : ");
str_text = br.readLine();
pp.process(file_name, str_text);
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process {
public String file_name="";
File create_file;
String show_text= "";
public void process(String str1, String str2)
{
try {
File file = new File(str1);
boolean success = file.createNewFile();
if (success){
file_name= file.getName();
show_text = str2;
System.out.println( "file name " + file_name);
System.out.println("Writing to this file");
char buff[] = new char[show_text.length()];
show_text.getChars(0,show_text.length(),buff, 0);
FileWriter fr = new FileWriter(str1);
for (int i =0; i <buff.length; i++)
{
fr.write(buff[i]);
}
fr.close();
read_file();
}
} catch (IOException e) { System.out.println("could not read");
}
}
public void read_file()
{
FileInputStream fr =null;;
try {
fr = new FileInputStream(file_name);
int line;
System.out.println("Reading from " + file_name);
while ((line = fr.read())!= -1)
{
System.out.print(line + "|");
}
fr.close();
} catch (IOException e) { System.out.println("could not read"); }
}
}
|
|
|
However,
|
|
Now let us look again when copying the contents of one file to another
using the byte streams. |
package ch4_package;
import java.io.*;
public class Test_This {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String file_name ="", str_text="";
Process pp = new Process();
try{
//System.out.println("---While Loop---");
System.out.print("Please Enter a File name: ");
file_name = br.readLine();
System.out.print("Now the text to be added : ");
str_text = br.readLine();
pp.process(file_name, str_text);
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process {
public String file_name="";
File create_file;
String show_text= "";
public void process(String str1, String str2)
{
try {
File file = new File(str1);
boolean success = file.createNewFile();
if (success){
file_name= file.getName();
show_text = str2;
System.out.println( "file name " + file_name);
System.out.println("Writing to this file");
char buff[] = new char[show_text.length()];
show_text.getChars(0,show_text.length(),buff, 0);
FileWriter fr = new FileWriter(str1);
for (int i =0; i <buff.length; i++)
{
fr.write(buff[i]);
}
fr.close();
read_file();
}
} catch (IOException e) { System.out.println("could not read");
}
}
public void read_file()
{
//FileReader fr =null;;
//FileWriter br = null;;
FileInputStream fr =null;
FileOutputStream out = null;;
//FileWriter br = null;;
try {
fr = new FileInputStream(file_name);
out = new FileOutputStream("temp.txt");
int line;
System.out.println("Reading from " + file_name);
while ((line = fr.read())!= -1)
{
System.out.print(line + "|" );
out.write(line);
}
fr.close();
} catch (IOException e) { System.out.println("could not read"); }
}
}
|
|
|
|
the files "new_test.doc" and "temp.txt " are created
|
Space takes 32 bytes Although it writes actual text to the file using
File Reader/Writer, "FileWriter fr = new FileWriter(str1);" and note
pad shows the text written as show below, the console out put shows the
corresponding bytes.
But the console out put shows the bytes taken by the words.
|
|
Next Example |
Note the file contents
|
package ch4_package;
import java.io.*;
import java.util.Scanner;
public class Test_This {
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String file_name ="", str_text="";
Process pp = new Process();
try{
//temp.txt needed
System.out.print("Please Enter a File name: ");
file_name = br.readLine();
pp.read_file(file_name);
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process {
public void read_file(String file_name)throws IOException
{
FileInputStream fr =null;;
int line, total_byte=0;
try {
fr = new FileInputStream(file_name);
System.out.println("Reading from " + file_name);
while ((line = fr.read())!= -1)
{
System.out.print(line + "|");
total_byte++;
}
fr.close();
} catch (IOException e) { System.out.println("could not read"); }
finally {
if (fr != null) {
fr.close();
System.out.println("total Byte " + total_byte);
}
}
}
}
|
|
|