Can someone check my Syntax?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Can someone check my Syntax?

  1. #1
    Join Date
    Dec 2002
    Posts
    7

    Can someone check my Syntax?

    I am supposed to Write a java program to open a text file. First I have to Check if the file already exists. If the file already exists then append data to it. Otherwise program should write 10 lines of text data in the file.
    Can anyone else check to see if this is right, i did this using Notepad

    <font size=2><pre>

    import java.io.PrintWriter;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.lang.String;
    import java.io.IOException;
    import java.io.FileWriter;

    public class textfile {

    public textfile() {
    }
    public int FileWrite(String FileName) //method that takes a string as parameters
    {
    PrintWriter OutFile = null;
    try
    {
    OutFile = new PrintWriter(new FileOutputStream(FileName));
    }
    catch(FileNotFoundException e)
    {
    System.out.println("Error in creating the file" + FileName);
    System.exit(1);
    }
    System.out.println("Type in 10 lines of text");
    for(int i=1;i<11;i++)
    {
    String Lines;
    Lines = ReadInput.ReadLine();
    OutFile.println(Lines);
    }
    OutFile.close();
    System.out.println("Stored the lines in the file:" + FileName);
    return 0;
    }
    public int AppendFile(String FileName)
    {
    PrintWriter OutFile = null;
    try
    {
    OutFile = new PrintWriter(new FileOutputStream(FileName, true));
    }
    catch(FileNotFoundException e)
    {
    System.out.println("Errorin finding or creating the file" + FileName);
    System.exit(1);
    }
    System.out.println("Type in 10 more lines of text");

    for(int i=1; i<11;i++)
    {
    String Lines;
    Lines = ReadInput.ReadLine();
    OutFile.println(Lines); //takes input from User
    }
    OutFile.close();
    System.out.println("File saved"); //assurance of the file saved
    return 0;
    }
    public void ReadFile(String FileName)
    {
    String Lines = null;
    try
    {
    BufferedReader Infil = new BufferedReader(new FileReader(FileName));
    Lines = InFile.readLine();
    while(Lines !=null) // this While loop is used to print out the contents of the file
    { System.out.println(Lines);
    Lines = InFile.readLine();
    }
    InFile.close();
    }
    catch(FileNotFoundException e)
    {
    System.out.println("Error opening file or file not found " + e); //prints file not found
    System.exit(1);
    }
    catch (IOException e)
    {
    System.out.println("Error reading file from " + FileName + e); //prints error message
    }
    }

    public static void main(String[] args) {
    textfile CFile = new textfile();
    CFile.FileWrite("C:\\NewFile.txt"); //when file exitsts call this method
    CFile.AppendFile("C:\\NewFile.txt");//calls Appendfile method
    CFile.ReadFile("C:\\NewFile.txt"); // calls Readfile method

    }
    }

    }

    </pre></font>

  2. #2
    Join Date
    Aug 2002
    Posts
    94
    Maybe the best way to check your code is to compile it and run it - if it runs fine then you may not feel the necessity of asking others!


    If there is any problem then post it the forum quoting specific details

  3. #3
    Join Date
    Jan 2003
    Posts
    5

    Syntax check.

    Hi,
    I think you are on the right track here, after a quick look. But you have some syntax errors.
    1. Lines = ReadInput.ReadLine();
    Is "ReadInput" a program that you wrote with a method ReadLine(). You need to define/write this method and program.
    2. Lines = InFile.readLine();
    Same here? Otherwise everything is fine. Also get an editor. A good one to start with is Eclipse(GREAT). From IBM and associates of course. You can get it free too!
    Hope this helps.
    See ya
    Maniac

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center