How to format text file?

How to format text file?

View Answers

December 17, 2008 at 4:30 AM

Hi friend,


I am sending running code. I hope that this code will help you.



import java.io.*;

public class ReadWriteTextFile {

static public String getContents(File aFile) {
StringBuilder contents = new StringBuilder();

try {

BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null; //not declared within while loop

while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}

return contents.toString();
}


static public void setContents(File aFile, String aContents)
throws FileNotFoundException, IOException {
if (aFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
if (!aFile.exists()) {
throw new FileNotFoundException ("File does not exist: " + aFile);
}
if (!aFile.isFile()) {
throw new IllegalArgumentException("Should not be a directory: " + aFile);
}
if (!aFile.canWrite()) {
throw new IllegalArgumentException("File cannot be written: " + aFile);
}

//use buffering
Writer output = new BufferedWriter(new FileWriter(aFile));
try {
//FileWriter always assumes default encoding is OK!
output.write( aContents );
}
finally {
output.close();
}
}

public static void main(String args[]) throws IOException {
File testFile = new File("/home/vinod/amarexamples/anju.txt");
System.out.println("Original file contents: " + getContents(testFile));
setContents(testFile, "The content of this file has been overwritten...");
System.out.println("New file contents: " + getContents(testFile));
}
}

-----------------------------------------

Visit for more information.

http://www.roseindia.net/java/example/java/io/java-read-file-line-by-line.shtml

Thanks.









Related Tutorials/Questions & Answers:
How to convert multiple files in java to .zip format
How to print differences of two text files
Advertisements
How to format text file? - Java Beginners
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix
how to search a text in all files in a folder in unix
How to Convert Text Files into Gzip File
ModuleNotFoundError: No module named 'filee'
ModuleNotFoundError: No module named 'filem'
ModuleNotFoundError: No module named 'filez'
Text Files
ModuleNotFoundError: No module named 'filex'
ModuleNotFoundError: No module named 'filex'
How could we get log file as simple text format
How can I Convert my Image Files to Text Files? - IDE Questions
autocomplete(): Spring mvc with jquery: I am not getting correct value in the text filed. Please help me
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
Converting Text Files into Bzip File
Converting the text files into gzip file.
How to create a new text file that contains the file names of the copied files in a new directory? - Java Beginners
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
save output in multiple text files
How to print this Format exactly?
How to format number in Java?
how to read the values for text and csv files and store those values into database in multiple rows..means one value for one row
unicode conversion to its actual text format
Converting the text files into bzip file.
how to print pdf format
Java show files in tree format
How create csv format in jsf ?
ModuleNotFoundError: No module named 'filer'
How Compare 2 xml files with JDOM - Java Beginners
How to convert EBCDIC format value into ASCII format value in java
How to list files in hadoop?
File format validation and text field validation in java swings
How to parse data in JSON format
How to make a cloudy text, cloudy text, text
How to format json String in Java
How to format json String in Java
ModuleNotFoundError: No module named 'flask-filer'
ModuleNotFoundError: No module named 'flask-filer'
ModuleNotFoundError: No module named 'flask-filer'
ModuleNotFoundError: No module named 'fluentcms-filer'
ModuleNotFoundError: No module named 'cmsplugin-filer'
ModuleNotFoundError: No module named 'django-filer'
ModuleNotFoundError: No module named 'flask-filer'
ModuleNotFoundError: No module named 'fluentcms-filer'
how to use stringtokenizer on table? and display in table format.
How to delete files in Java?

Ads