Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

import java.io.

*; // Import the File class


import java.io.IOException; // Import the IOException class to handle errors

public class CreateFile {


public static void main(String[] args) {
try {
File infile = new File("source.txt");
File outfile=new File("dest.txt");
infile.createNewFile();//you have to reate the source file

//writing into a file from the IDE itself using an array of bytes bcs it's
FileOutputStream not FileWriter
String test="gjkjgfjddhhhgdfgsys hstrsb shgshbg ";
byte content[]=test.getBytes();
FileOutputStream temp=new FileOutputStream(infile);
temp.write(content);
temp.close();

//writing into a file from onther file


FileInputStream in=new FileInputStream(infile);
FileOutputStream out=new FileOutputStream(outfile);
int c;
while((c=in.read())!=-1){
out.write(c);
System.out.println(c + ", " + (char)c);
}
in.close();
out.close();

//reading the things wev'e written in the file dest


FileInputStream temp2=new FileInputStream(outfile);
while((c=temp2.read())!=-1){
System.out.print((char)c);
}
temp2.close();

} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

You might also like