Home Tutorialhelp Comment Something more useful

 
 

Comment

Wes
Something more useful,
October 22, 2008 at 9:47 PM

I prefer to have a file that I can write to like a log file such as appending or prepending text to the file. Here is an example that may be useful to someone.

import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.BufferedReader;

class File {

public static void writeln(String content, char type) {
File.write(content+"\n", type);
}

public static void write(String content, char type) {
try {
String outfile = "logs/out.txt";
System.out.println("Writing to file: "+outfile);
if(type == 'a' || type == 'p') { // needs input from the file first
String in = File.read(outfile);
if(type == 'a') {
System.out.println(" Prepending Content: "+content);
content = in+content;
} else if (type == 'p') { // prepend
System.out.println(" Prepending Content: "+content);
content = content+in;
}
}
// Create file
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
System.out.println("Writing Content: "+content);
// if !append or prepend then the file is overwritten. otherwise it contains the text in front of it or behind it for what was added.
out.write(content);
// Close the output stream
out.close();
} catch (Exception e) { // Catch exception if any
System.err.println("Error: "+e.getMessage());
System.exit(-1);
}
}

public static String read(String infile) {
try {
String content = new String();
BufferedReader in = new BufferedReader(new FileReader(infile));
String read = in.readLine();
while(read != null) {
if(read.length()>0 && read.charAt(read.length()-1) != '\n') {
read += "\n";
}
content += read;
read = in.readLine();
}
in.close();
System.out.println("Read Content: "+content);
return content;
} catch (Exception e) { // Catch exception if any
System.err.println("Error: "+e.getMessage());
System.exit(-1);
}
return new String();
}
}

View All Comments | View Tutorial
Advertisement
Related Tutorial and Articles
Java Beginners

How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java... bufferedWriter object to write in the file. For more details and examples related to How to write in File of Java Program 

Java Beginners

more circles - Java Beginners
more circles  Write an application that uses Circle class you created in the previous assignment. ? The program includes a static method createCircle() that o reads a radius of a circle from the user o creates a circle object 

Java-Tutorials

How to Write to a File in Java without overwriting
Learn how to Write to a File in Java without overwriting in your Java program This tutorial teaches you how to write to a file in Java without overwriting... which already contains the data and our program will write to this file without 

Java Beginners

How to write to xml file in Java?
How to write to xml file in Java?  Hi Friends, Can anyone help me how to write in xml file in Java programming. Please fill free to give example or reference website for getting example.   Hi, To write in xml file 

Java Beginners

How to Write To File BufferedWriter in Java
How to Write To File BufferedWriter in Java  how to write to file bufferedwriter in Java   In Java there are 2 types of class files used...() method's opeation, which take more time to write. Here is the suggested link 

Java Beginners

how to write append file in Java
how to write append file in Java  How to write append file in Java... existing file should be retained when you are trying to write new contents...("appenToFile.txt", true); For More Details visit this link: How to Append file 

Java Beginners

even more circles - Java Beginners
even more circles  Write an application that compares two circle objects. ? You need to include a new method equals(Circle c) in Circle class. The method compares the radiuses. ? The program reads two radiuses from the user 

Java-Tutorials

Java Write to File
Java Write to File In this tutorial you will learn how to write to file in java. Write to a file in java we are required to use some classes of java.io... Writer because, some Writer's write() method's operations may take more 

Java Beginners

write program - Java Beginners
write program  write a java program that will read the values...]); } System.out.println(); } } } For more information on java visit to : http://www.roseindia.net/java/beginners/ Thanks 

Java Beginners

Java file read write operation
Java file read write operation  how to read and write the data from text file.Suppose i have text file with 4 fields name ,roll no ,marks1,marks2 with more than 20 records......i need to store these value in object and pass 

Java Beginners

How to write in File in Java
How to write in File in Java  Hi, How to write in File in Java. Please suggest and give example of this program. thanks 

Java Beginners

Java write to file
Java write to file  How to write to a file in Java? Is there any... to a text file. Here is the examples: Example program to write to file. File Handling examples in Java Java Input/Output Examples How to read file in Java 

Struts Questions

struts - application missing something?
struts - application missing something?   Hello I added a parameter... and they are taken to correct location. What am I missing? The JSP, Java Class... to working in the presentation layer no deeper than struts-config. I know Java 

Java Beginners

How to Write to file in Java?
How to Write to file in Java?  How to Write to file in Java Program?   Hi, For writing a file in Java, we need some classes from Java.IO... the Examples of How to write to File in Java Program: WriteToFileExample.java import 

Java-Tutorials

Java Write To File BufferedWriter
Java Write To File BufferedWriter In this tutorial you will learn how to write... to write into the file by java program. Download Source Code... Writer because, some Writer's write() method's operations may take more 

Java Beginners

How to write to file using FileOutputStream
How to write to file using FileOutputStream  Hi friends, Please help me in java program. How to write to file using FileOutputStream? thanks,   Hi, To write a file using FileOutputStream, we have to use 

Java-Tutorials

Java Write To File From String
Java Write To File From String In this tutorial you will learn how to write to file from string. Write to file from string using java at first we will have... by you with containing the text that you are trying to write in that file by java 

JSP Tutorials

JSP Tutorials Resource - Useful Jsp Tutorials Links and Resources
of client-side Java in this discussion just for beginners. Core topics include... are: HTML.  You should be able to put together HTML pages. Java.  You should be able to program in Java. This tutorial teaches JSP 

Java Beginners

File Writing - Java Beginners
File Writing  Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel file from console window... ... visit for more information. http://www.roseindia.net/jsp/poi 

Java Beginners

Write an applet that prints "Lear Java it is useful" at the current cursor position whenever the mouse left button is clicked.
Write an applet that prints "Lear Java it is useful" at the current cursor position whenever the mouse left button is clicked.   Write an applet that prints "Lear Java it is useful" at the current cursor position whenever 

Java Beginners

how to write this program? - Java Beginners
how to write this program?  (game:scissor,rock ,paper) A program that plays the sciccor-rock-paper game.Revise the program to let the user continuously play until either the user or the computer wins more than 4 times 

Java Beginners

How to write file by line in Java
How to write file by line in Java  How to write file by line in Java   Hi, For Write to a file by line using java programming language we... in a new line. How to use Write To File By Line in Java programs 

Java Beginners

How to write into CSV file in Java
How to write into CSV file in Java  How to write into CSV file in Java   Hi, To create Comma separated value(CSV) file is very simple... passed the file name to write into a stream characters. Then we use the FileWriter 

Java Beginners

Read and write file
Read and write file  HI, How to read and write file from Java program? Thanks   Hi, See the following tutorials: Java Write To File Read File Thanks 

Java Beginners

more doubts sir. - Java Beginners
more doubts sir.  Hello sir, Sir i have executed your code... in the bottom of the page.sir i also need to add some more buttons as in internet exoplorer such as search bar and some more buttons.Sir help me out 

Java Beginners

CREATE AND WRITE FILE THREAD JAVA
CREATE AND WRITE FILE THREAD JAVA  Hi guys I was wondering how can I make this program in java with threads. I need to create a file and write in it (i know how to do that) but by listening in some port all the data that is being 

Java Beginners

how to write to file primitive type double
how to write to file primitive type double  Hi, How to write to file primitive type double in java program? thanks   Hi, To write... in the output stream as a 8 byte quantity. For more about File primitive data type 

Java Beginners

write following program
-threading. Explain with examples Q2 Write a java applet that takes your name through a parameter from the HTML file and displays it Q3 Write a short note... the state transition diagram of a thread Q6 Write a java applet that displays 9 

Java Beginners

write java code discount bookstore - Java Beginners
write java code discount bookstore  The question Cik Mat operates... with special discounts, while others are sold at normal price. Write a program which... if the book is sold at normal price). Write appropriate constructors 

Java Beginners

how to write Session Handling - Java Beginners
how to write Session Handling  can any one please help me....how to handle Sessions .. in java Programs... with example  Hi friend, Read for more information with example at: http://www.roseindia.net/servlets 

Java Beginners

write a program - Java Beginners
write a program  1. write a program that reads a string composed of 6 words then generate and print a word composed of the first letters of the 6 words.  Hi friend, Code to solve the problem : class FirstLetter 

Java-Tutorials

Java Write To File By Line
Java Write To File By Line In this tutorial you will learn how to write to file by line Write to a file by line using java you can use the newLine() method... that you are trying to write in that file by java program like as : Download 

Java-Tutorials

How to Write to a File in Java
In this Java tutorial, we will demonstrate how to write to a file in Java... to a file from Java program. To write the Java Write To File we will use two... Java" into out.txt file. While creating the object of FileWriter class we 

Java Beginners

How to write a session code - Java Beginners
How to write a session code   Once Again Thanks Deepak...Thanks... to write a session code plz write a session code and post answer my personal id... ------------------------------------------------- Read for more information 

JSP Servlet Questions

write xml file with jsp useBean - JSP-Servlet
write xml file with jsp useBean  how to write into xml files with jsp... allows our application to obtain a Java XML parser. DocumentBuilderFactory... an org.w3c.dom.Document from XML. Save the xml file at bin file of C:\apache-tomat-5.5.23\bin 

Java Beginners

IO File - Java Beginners
IO File  Write a java program which will read an input file & will produce an output file which will extract errors & warnings. It shall exclude... from the log file. This is the task assigned to me..Please help me 

Java-Tutorials

Java Write To File CSV
Java Write To File CSV In this tutorial you will learn how to write to file... with containing the text that you are trying to write in that file by java program like... that is in a tabular form. In java to create a csv file is simple, you would require 

Java Beginners

How to write a error.jsp code - Java Beginners
How to write a error.jsp code  Thanks once again I have a session code pls help me that how can i write a error.jsp page. why use error.jsp...let me know that hw can i write the error.jsp code. plz send me error.jsp code 

Java-Tutorials

Java Write To File Binary
Java Write To File Binary In this tutorial you will learn how to write... types can be formed to a byte of 8 bits. Write to a binary file in java... with containing the text that you are trying to write into the file by java program 

Java Tips and Examples

Java Write To File - Java Tutorial
Java Write To File - Java Tutorial Learn how to write to a file from... on a FileOutputStream classes to write data to a file from Java program... of Java Tutorial you will learn how to write java program to write to a file. We 

Java Beginners

How to write in Binary file in Java programming language
How to write in Binary file in Java programming language  I need some help about Java programming. Please explain me "How to write in Binary file in Java programming language 

Java Beginners

What is the best way to write to file in Java?
What is the best way to write to file in Java?  I am creating... to write to a text file in Java? Since my program will continuously write the data...;Read the tutorial at Java Write To File - Java Tutorial. Thanks 

Java Beginners

file handling - Java Beginners
manner. now write a progam in java and create another file which is having the amount required to purchase each item and at last read the newly created file...file handling  assume two files are there- 1. with the quanity of he 

General

Java write file
Java write file       Following example demonstrates, how to create and write a file... class.           Write_File 

Java Beginners

how to write to file from string in Java
how to write to file from string in Java  Hi, Please any one help me for how to write to file from string in Java. I am beginner in Java programming. Thanks,   Hi, Are you eager to learn how to write to file from 

Java Beginners

jar file - Java Beginners
file. what is the manifest file, and how to create it. And tell me more about... it more usable,. JAR file size is very small as compared to the class file. So... an application packaged as a JAR file (requires the Main-class manifest header)java -jar 

Java-Tutorials

Java Write To File FileWriter
Java Write To File FileWriter In this example you will learn how to write... the text that you are trying to write in that file by java program like... that retain the older text if any, when it is trying to write new text in that file 

Java Beginners

how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java 

Java Beginners

File Handling - Java Beginners
"); } } } I have few files in that directory.. my need is to select one file and write few... again i can able to write datas into the file but the previous content...File Handling  Thanks for replying me.. but i am sorry to say 

Java-Tutorials

Java Write To File FileOutputStream
Java Write To File FileOutputStream In this tutorial you will learn how to write to file using FileOutputStream. Write to file using FileOutputStream you... with containing the text that you are trying to write in that file by java 

Java-Tutorials

Java Write To File - Java Tutorial
an internal FileOutputStream to write bytes to the specified file... the file into specified directory. Following code  write data into new... C:\nisha>java CreateFile Please enter the file name 

Java Beginners

File - Java Beginners
. Here it is: Write a program to create a file named "numbers.dat". Then create... the file, and display the results. Write all even numbered integers from 1... by a comma. After the file has been created, close and reopen the file and display 

Java Beginners

java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my... xml file and storet that in particlar location. Please help me out Thanks 

Java Code Examples

Java Write to properties file
Java Write to properties file In this section, you will learn how to write data to properties file.  The properties file is basically used to store.... Here we are going to create a .properties file and write data 

Java Beginners

How to write file from inputstream in java
How to write file from inputstream in java  Hi, how to write file from inputstream in java program. Plz suggest an example or suggested link... link to your query "How to Write to inputStream in Java". This is good 

Java Beginners

File Handling - Java Beginners
and password.now my problem is that whenever i write datas into the file the previous...("File create and write example."); BufferedReader buff = new BufferedReader(new...File Handling  I have written one program by which we can create 

Java-Tutorials

Java Write To File Double
Java Write To File Double In this tutorial you will learn how to write to file primitive type double. Write to file primitive data type double... with containing the text that you are trying to write into the file by java program 

Java Beginners

how to write to file at the end of the line
how to write to file at the end of the line  Hi, Where do i learn how to write to file at the end of the line using Java program. Plz suggest... will help you for "How to write the file at the end of line using Java language 

Java Beginners

To read & write a excel file using the core java
To read & write a excel file using the core java  Hai, I'm new to JavaProgram.But now i need java program to read & write a excel file so, can anyone help me to learn the above mentioned topic(link for the portion 

Java Beginners

More than 1 preparedStatement object - Java Beginners
java... Thanks  Hi Friend, You can use more than one prepared Statement object in the java class.Try the following code: import java.sql....More than 1 preparedStatement object  Hey but I want to use more than 

Java Beginners

how to write file from FileInputStream
. Thanks,   Hi, For Write to file from FileInputStream in java you may use... this website Write to file from FileInputStream in java...how to write file from FileInputStream  Hi, How to Write a file 

Java-Tutorials

How to write a file in Java?
How to write a file in Java? To write a file in Java use the class FileWriter... and strings. Example of how to write text to a file in java: import java.io.... will make a text file by the name "out.txt" and will write "Hello Java 

Java Beginners

File Handling - Java Beginners
File Handling  Q. Write a java prg which accepts a list of existing...); fout2.write(ch);//change case of text of f1 and write to f2 } fin1.close...(e.getMessage()); } } }   its prg of copying file Check code 

Java Beginners

open a bufferedwriter file in append mode - Java Beginners
( ); bwObj.close( ); System.out.println( "File is Write...open a bufferedwriter file in append mode  hi.. i jus want knw how to opena bufferedwriter file in append mode..  Hi friend, FileWriter 

Java Beginners

Setup file - Java Beginners
Setup file  Hello sir ,how i can make Java Programs Set up File.... Point to note here is that the Java compiler very well knows about... second) throws Exception{ For more information,please visit the following link 

Java Beginners

write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter 

Java Beginners

How to write to file using FileWriter
How to write to file using FileWriter  hi, How to write to file using FileWriter thanks,   Hi, To writing in a file in Java program we...(File file), FileWriter(File file, boolean append), FileWriter(FileDescriptor fd 

Java Tips and Examples

Write Text into File
Write Text into File      ... this example we are initialize string to write into file. We are creating file... to write text into file. At last close output file using close() method 

Java-Tutorials

Java Write To File From FileInputStream
Java Write To File From FileInputStream In this tutorial you will learn how to write to file from FileInputStream. Write to file from FileInputStream in java... input bytes from a file or can say it reads the streams of bytes. And to write 

Java Beginners

how to write the program - Java Beginners
how to write the program  WAP to create the report card user input Name, Class, Division, Roll no., Marks obtained in following subjects Lang Hindi History Geography Math Phy Chem. Bio Eve CSTA Also has a back up 

Java Beginners

WRITE A PROGRAM IN JAVA
WRITE A PROGRAM IN JAVA  How do I write a program in Java, have the program display a message with your name in it and a number (Hello john Smith!1... ten contiguous output statement. For example your output cuold be something like 

General

Write Keys and Values to the Properties file in Java
Write Keys and Values to the Properties file in Java... how to write keys and values in the properties files through the Java program... not found!" and then gives you one more chance for entering the file name 

General

Write to a file
Write to a file   ...; the BufferedOutputStream class that store the data in an internal buffer and lets you write...; to the existed file. The given example uses the BufferedOutputstream 

Java Beginners

a java program - Java Beginners
for more information. http://www.roseindia.net/java/beginners/PrimeNumber.shtml... a java program  well sir, i just wanna ask you something regarding that if the question say .....write a program to check whether a number is even 

Java Code Examples

Java write to file line by line
Java write to file line by line In this section, you will learn how to write a file line by line. It is better to use Writer class instead of OutputStream class if you want to output text to the file since the purpose of Writer classes 

Java Technology Tutorials

Java write even numbers to file
Java write even numbers to file In this section, you will learn how to write the even numbers to file. By using the PrintWriter class, you can write any type of data to the file. It has wide variety of methods for different primitive 

Java Beginners

file in java - Java Beginners
file in java  I want to count the number of white line and comment line in a file (.sql) with java if you have an example thank you in advance 

Java Beginners

java-jar file creation - Java Beginners
java-jar file creation  how to create a jar file?i have a folder... file  Hi Friend, JAR stands for the Java Archive. This file format is used to distribute a set of java classes. This file helps you to reduce the file 

Java Technology Tutorials

FTP File Upload in Java
FTP File Upload in Java This tutorial shows you how you can write Java program... easy to write code for FTP File Upload in Java. I am assuming that you have FTP... will be able to create programs in Java that uploads the file on FTP server 

Java Beginners

how to use Excel Templet to write excel file using java.
how to use Excel Templet to write excel file using java.  how to use Excel Templet to write excel file using java 

Java Beginners

Write a program in Java to append content at the end of an already existing file.
Write a program in Java to append content at the end of an already existing file.  Write a program in Java to append content at the end of an already existing file 

Java Beginners

read excel file from Java - Java Beginners
read excel file from Java  How we read excel file data with the help of java?  Hi friend, For read more information on Java POI visit to : http://www.roseindia.net/java/poi/ Thanks 

Java-Tutorials

Java Write To File Dom Document
Java Write To File Dom Document In this tutorial you will learn how to write to file dom document. Write to xml file you would be required to use the package javax.xml. This package provides the facility to create XML file easily 

Java Beginners

To find palindrome no in a file - Java Beginners
To find palindrome no in a file  hi all i am having a problem...I wanted to write one java program where i have to count the no of palindrome in a file. I tried it with my own but not able to get result.pls help me out   

Java Beginners

Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The file contains nothing but strings 

Java Beginners

how to use Excel Template to write excel file using java
how to use Excel Template to write excel file using java  How to use Excel template to write data in that file using java 

Java-Tutorials

Java Write To File End Of Line
Java Write To File End Of Line In this tutorial you will learn how to write to file at the end of the line. Write to a file in which some contents... into which I have written some text. Now to write new text in a file 

Java Beginners

java - Java Beginners
java  What is SCJP and write its benefits. How it is useful for JAVA programmers.  Hi Friend, Sun Certified Java Programmer... interested in demonstrating proficiency in the fundamentals of the Java 

Java Beginners

java - Java Beginners
java   What is SCJP and write its benefits. How it is useful for JAVA programmers.  Hi Friend, Sun Certified Java Programmer... interested in demonstrating proficiency in the fundamentals of the Java 

Java-Tutorials

Applet Write Files Example
;  In this section, you will learn how to write to a file from... a Java security policy file that will allow the granted permission for writing a file from an applet. Without maintaining the Java security policy file 

Java Beginners

Java Stream Write Exception
Java Stream Write Exception  I am doing socket programming. I have two files called Server.java and client.java. Both programs were running... lines to client.java file: //old line out.writeUTF(dbdata); //dbdata 

Java Beginners

Java File - Java Beginners
Java File  Hi Friend, Thank you for the support I got previously... Anyone please send me the Java Code for scanning a directory and print.... Output Print the file names and size of the file to the console. Please 

Java Beginners

Create JAR file - Java Beginners
to make my .class files to get converted into .jar file. More knowledge.... But do let me know, isn't there any other way of making a JAR File. Simply like... each time in order to run your JAVA program on some other computer, it;s better 

Java Beginners

how to write tis java programming - Java Beginners
how to write tis java programming   Airport Transport Service...; displays the details of facility how to write tis program in java.....pls reply.... Using Transport.class and Facility.class, write a program that displays 

Java Beginners

How to write more than 65536 rows in single excel file but more than 1 sheets?
How to write more than 65536 rows in single excel file but more than 1 sheets?  Hi, i want to write more than 65536 rows in single excel file but more than 1 sheets.. now i can write only less than 65536 rows.. but in my office 

Java Beginners

Read Write
and write to one text file   Java Read Multiple Files and store the data into another text file The given code reads all the text files of the directory and stored the data into another text file. import java.io.*; import 

Java Beginners

Writing xml file - Java Beginners
(); bw.close(); } } For more information on Java visit... XmlServlet().createXmlTree(doc); System.out.println("Xml File Created... = sw.toString(); File file = new File("c:/newxml.xml 

Java Beginners

java code to upload and download a file - Java Beginners
hos a.doc  Hi friend, For more information on File Upload...java code to upload and download a file  Is their any java code to upload a file and download a file from databse, My requirement is how can i 

Java Beginners

How to write file text In New Line
to write file text in New line in Java...How to write file text In New Line  Hi friends, How to write file text in new line in Java program. How to write the syntax for this with example 

Java Beginners

write simple java code - Java Beginners
write simple java code  Write a program that creates a simple calculator .The user enters two numbers in the text fields, Number 1 and Number 2. There are 4 buttons, labeled with ?Add?, ?Substract?, ?Multiply? and ?Divide 

Advertisements Advertisements
 

 

 

DMCA.com