i had wrot this code , and i would like to share it with you :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class FileRead{
// condtractore---------------------------------------------------------------
public FileRead()
{
setFileName("");
}
// condtractore---------------------------------------------------------------
public FileRead(String FileName)
{
setFileName(FileName);
}
//the variable
private File file;
// set a file name------------------------------------------------------------
public void setFileName(String FileName)
{
file = new File(FileName);
}
public boolean isExsists()
{
if (file.exists()){return true;}
else
{
System.out.println("File Does not exsist");
return false;}
}
//read a file-----------------------------------------------------------------
public String readFile()
{
if(!isExsists()){return null;}
try{
BufferedReader input = new BufferedReader(new FileReader(this.file));
String text= new String("");
StringBuffer buffer = new StringBuffer();
while((text = input.readLine()) != null)
{
text +="\n";
buffer.append(text);
}
return buffer.toString();
}// end try
catch(IOException ioException)
{
JOptionPane.showMessageDialog(null,"File Error","File Error",JOptionPane.ERROR_MESSAGE );
return "";
}// end catch
}//end read a file method
// read file directrly--------------------------------------------------------
public String readFile(String FileName)
{
this.setFileName(FileName);
return (this.readFile());
}// end read file directrly
//pritn the content of a file-------------------------------------------------
public void printFile()
{
String OutPut = new String("");
OutPut = this.readFile();
System.out.println(OutPut);
}// end pritn the content of a file
//pritn the content of a file-------------------------------------------------
public void printFile(String FileName)
{
String OutPut = new String("");
OutPut = this.readFile(FileName);
System.out.println(OutPut);
}// end pritn the content of a file
// the main method
public static void main(String[] args) {
System.out.println("\n\n\tStart the main Method:\n");
// the call for the classes start from hear ---VV--V--Vv ----vV-------------
FileRead file = new FileRead("MyFile.txt"); //<-- "MyFile.txt" is name of an exsisting file
file.printFile();
// the call for the classes end hear ------------^^-^^^---^-^^^^^-----------
}//end the main method
}// end FileRead classand if there is any questions about the code , just ask :)