sample:
Input file: input.txt
Input file: input.txt
class Employee
{
private String name;
public Integer age;
protected Date dob;
public Address addr;
private Date doj;
public String designation;
}
output file: output.txt(Empty file)
InputFile.java
package com.test;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import java.io.FileReader;
import
java.io.FileWriter;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.ObjectOutputStream;
import
java.util.ArrayList;
public class InputFil
{
public static void main(String[]
args) throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter
File Name:");
String fname=br.readLine();
try
{
@SuppressWarnings("unused")
FileInputStream f=new
FileInputStream(fname);
System.out.println("*****************************************************");
System.out.println("content
of file are:");
System.out.println("************************");
FileReader file=new
FileReader(fname);
BufferedReader b=new
BufferedReader(file);
String s1=b.readLine();
while(s1!=null)
{
System.out.println(s1);
s1=b.readLine();
}
}
catch(FileNotFoundException
e)
{
System.out.println("File not
Found");
return;
}
System.out.println("*****************************************************");
ArrayList<String>rows = new
ArrayList<String>();
try {
System.out.println("*****************************************************");
System.out.println("In Array
List :");
System.out.println("*******************");
BufferedReader r = new BufferedReader(new FileReader(
fname));
String line = null;
while ((line =
r.readLine()) != null)
{
String[] arr=line.split("\\s+");
rows.add(line);
}
System.out.println(rows.toString());
} catch (IOException e)
{
e.printStackTrace();
}
BufferedWriter writer=null;
writer = new BufferedWriter(new FileWriter("output.txt"));
//ObjectOutputStream out = new
ObjectOutputStream((output));
String listWord;
for (int i = 0; i<
rows.size(); i++)
{
listWord = rows.get(i);
writer.write(listWord);
writer.write("\n");
}
writer.close();
}
}
output of program
Enter File Name:
input.txt
*****************************************************
content of file are:
************************
class Employee
{
private String name;
public Integer age;
protected Date dob;
public Address addr;
private Date doj;
public String designation;
}
*****************************************************
*****************************************************
In Array List :
*******************
[class Employee, {, private String name;, public Integer age;, protected Date dob;, public Address addr;, private Date doj;, public String designation;, , }]
open output.txt (we can see all data in input.txt now available in output.txt)
class Employee
{
private String name;
public Integer age;
protected Date dob;
public Address addr;
private Date doj;
public String designation;
}
input.txt
*****************************************************
content of file are:
************************
class Employee
{
private String name;
public Integer age;
protected Date dob;
public Address addr;
private Date doj;
public String designation;
}
*****************************************************
*****************************************************
In Array List :
*******************
[class Employee, {, private String name;, public Integer age;, protected Date dob;, public Address addr;, private Date doj;, public String designation;, , }]
open output.txt (we can see all data in input.txt now available in output.txt)
class Employee
{
private String name;
public Integer age;
protected Date dob;
public Address addr;
private Date doj;
public String designation;
}
No comments:
Post a Comment