الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Read From File

مغلق
بدأه المبرمج1 في 10 مارس 2004 · 2 رد · 616 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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 class

and if there is any questions about the code , just ask :)

#2

جزاك الله خير يا اخوي

انا فعلا كنت محتاج هالكود

تسلم ايدك

وإن شاء الله لي جربته

باراويك شغلي :)

تحياتي

رحال

#3

رحال اهلا بك معنا.... في قسم الجافا....

انت جديد هنا ممكن تعرفنا عن نفسك

Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why

هذا الموضوع مغلق.

مواضيع مشابهة