Hi All,
Below is the code for :
1. Reading data from Notepad..
2.If Notepad exist then append data in it after reading data from other notepad else create new notepad and write data in it.
Below is the code for :
1. Reading data from Notepad..
2.If Notepad exist then append data in it after reading data from other notepad else create new notepad and write data in it.
import java.io.*;
public class notepad
{
public static void main(String args[])
{
notepad note=new notepad();
note.readNotepad();
}
public void readNotepad()
{
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("D://new_name.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
System.out.println (strLine);
writeNotepad(strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public void writeNotepad(String data) throws IOException
{
File sampleFile = new File("D://old_name.txt");
if (sampleFile.exists())
{
FileWriter fw = new FileWriter(sampleFile, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(data+" ");
bw.close();
}
else
{
sampleFile.createNewFile();
FileWriter fw = new FileWriter(sampleFile);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("new File's data "+data);
bw.close();
}
}
}
No comments:
Post a Comment