This java program,creates a text file in the address specified if is not present and writes the content in this text file.If the text file is already present in the specified address, the content will be appended to the previously existing content.This program requires two class file,one is the java main class file(WriteText.java) and other is the java class file(WriteFile.java) which wites the content into the text file.Steps are given below.
Steps- Select Java form the categories and Java Application from the projects, click Next.
- Set the Project name as writeText and set the Create Main Class, click Finish.
- Under the project "writeText", you can see two files, double click the Source Packages,you can see a package by name " writetext " ,double click it you will find the Java Main Class File, WriteText.java.
- Right click the package by name " writetext " and select New -> Other
- From Categories select Java and from File Types select Java Class, click Next
- Set the Class Name as "WriteFile.java" and ensure the package name is " writetext".
------------------------------------------
WriteText.java----------------------------------------------------------
package writetext;
import java.util.Scanner;
/**
*
* @author ZISSAN
*/
public class WriteText {
public static void main(String[] args) {
try {
System.out.println("Enter the address of text file::");
Scanner in = new Scanner(System.in);
String address = in.nextLine();
//enter the address in this format "C:/songs/newtest.txt"
//"newtest.txt" is the text file,if "newtest.txt"
//is not present,this program will create this text
//file in this address, "C:/songs/"
WriteFile data = new WriteFile(address,true);
//"WriteFile(address,true)" has two arguments,
//"address" specifies the address of the text fule,
//"true" is aboolean value,specifies to append
//the below content with the existing content
System.out.println("Enter your text::");
String content=in.nextLine();
//"content" is a variable of string datatype,
//holds the content that to be stored in
//assigned text file.
data.writeToFile(content);
} catch (Exception er) {
System.out.println("error"+er.getMessage()) ;
}
}
}
Now open the WriteFile.java, remove the existing code and paste the following code :
package writetext;
import java.io.*;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
*
* @author ZISSAN
*/
class WriteFile {
private String path = "";
private boolean append_to_file = false;
//WriteFile() is the being called in
//java main class file,Text.java
public WriteFile(String file_name) {
path = file_name;
}
public WriteFile(String file_name, boolean append_value) //two constructor are used
//first one is used when an user wants to overwrite the file
//2nd one is used when an user wants to add xtra in the file
{
path=file_name;
append_to_file=append_value;
}
public void writeToFile(String textLine) throws IOException{
FileWriter write=new FileWriter(path,append_to_file);
PrintWriter print_line=new PrintWriter(write);
print_line.printf("%s"+"%n",textLine);
print_line.close();
}
}
package writetext;
import java.util.Scanner;
/**
*
* @author ZISSAN
*/
public class WriteText {
public static void main(String[] args) {
try {
System.out.println("Enter the address of text file::");
Scanner in = new Scanner(System.in);
String address = in.nextLine();
//enter the address in this format "C:/songs/newtest.txt"
//"newtest.txt" is the text file,if "newtest.txt"
//is not present,this program will create this text
//file in this address, "C:/songs/"
WriteFile data = new WriteFile(address,true);
//"WriteFile(address,true)" has two arguments,
//"address" specifies the address of the text fule,
//"true" is aboolean value,specifies to append
//the below content with the existing content
System.out.println("Enter your text::");
String content=in.nextLine();
//"content" is a variable of string datatype,
//holds the content that to be stored in
//assigned text file.
data.writeToFile(content);
} catch (Exception er) {
System.out.println("error"+er.getMessage()) ;
}
}
}
-------------------------------------------------end of WriteText.java----------------------------------------------------
Now open the WriteFile.java, remove the existing code and paste the following code :
--------------------------------------------
WriteFile.java---------------------------------------------------------
import java.io.*;
import java.io.FileWriter;
import java.io.PrintWriter;
/**
*
* @author ZISSAN
*/
class WriteFile {
private String path = "";
private boolean append_to_file = false;
//WriteFile() is the being called in
//java main class file,Text.java
public WriteFile(String file_name) {
path = file_name;
}
public WriteFile(String file_name, boolean append_value) //two constructor are used
//first one is used when an user wants to overwrite the file
//2nd one is used when an user wants to add xtra in the file
{
path=file_name;
append_to_file=append_value;
}
public void writeToFile(String textLine) throws IOException{
FileWriter write=new FileWriter(path,append_to_file);
PrintWriter print_line=new PrintWriter(write);
print_line.printf("%s"+"%n",textLine);
print_line.close();
}
}
-----------------------------------------end of WriteFile.java----------------------------------------------------
Nice start zissan.. keep going buddy. You can check my Web-design blog.
ReplyDelete