-
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>
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks