Saturday, 21 June 2014

Find Character in file and remove from file the file in JAVA

  • First create two files with name S1.txt and S2.txt in E: drive
  • This program replace all occurrence of 'a' or 'A' character from file S1 and write into S2 file


               File file1 = new File("E:\\S2.txt");
File file2 = new File("E:\\S2.txt");
FileInputStream fis1 = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
StringBuffer sub1 = new StringBuffer();
int ch;
if(file1.exists())
{
while((ch=fis1.read()) != -1)
{

if((char)ch != 'a' && (char)ch != 'A')
{
 fos.write((char)(ch));
}
 
 
}
fis1.close();
 
}
int ch2;
FileInputStream fis2 = new FileInputStream(file2);
if(file2.exists())
{
while((ch2=fis2.read()) != -1)
{
sub1.append((char)ch2);
}
}

System.out.println("File Created Without A Character");
System.out.println(sub1);
 
 
}

No comments:

Post a Comment