Results 1 to 2 of 2
Thread: Create a Text file with java.
- 10-06-2012, 03:20 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 2
- Rep Power
- 0
Create a Text file with java.
hi friends
i am new in java . I'm tring to know java thats why i'm reading a book "Java: How to Learn program by Detiel and detiel " .there is code for Create Text file for sequential data access in this book's chapter 17.
here i can create text file but i could not input any data in that file . When input data and type <ctrl>z it shows me" invalid data". I think the try block of AddRecord method doesnot work .Where is my wrong ? can any body help me? here my code:
Java Code:public class AccountRecord { private int account; private String firstName; private String lastName; private double balance; public AccountRecord(){this(0," "," ",0.0);} public AccountRecord(int acct,String first,String last,double bal ) {setAccount(acct);setFirstName(first);setLastName(last);setBalance(bal);} public void setAccount(int acct){account=acct;} public int getAccount(){return account;} public void setFirstName(String first){firstName=first;} public String getFirstName(){return firstName;} public void setLastName(String last){lastName=last;} public String getLastName(){return lastName;} public void setBalance(double bal){balance=bal;} public double getBalance(){return balance;} }Java Code:package createtextfiletest; import com.sun.xml.internal.ws.api.pipe.NextAction; import java.io.FileNotFoundException; import java.util.Formatter; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class CreateTextFile { public Formatter output; public void OpenFile() { try { output = new Formatter("Clients.txt"); } catch (FileNotFoundException ex) { System.err.printf("\n File Not Found"); System.exit(1); } } public void AddRecord() { Scanner input = new Scanner(System.in); AccountRecord rcd= new AccountRecord(); System.out.printf("\nFor windows type <ctrl> z press enter for end of data indicator\n"); System.out.printf("Enter account no>0,firstname,lastName, and balace\n"); while(input.hasNext()) { try { rcd.setAccount(input.nextInt()); rcd.setFirstName(input.next()); rcd.setLastName(input.next()); rcd.setBalance(input.nextDouble()); if(rcd.getAccount()>0){ output.format("%d %s %s %.2f\n",rcd.getAccount(),rcd.getFirstName(),rcd.getLastName() ,rcd.getBalance()); } else{ System.out.printf("\n Invalid input. Try again"); input.nextLine(); } } catch(Exception X){ System.out.printf("\n Invalid data\n"); input.nextLine(); System.out.printf("Enter account no>0,firstname,lastName, and balace\n"); } } } public void CloseFile() {if (output !=null) output.close(); } }
Java Code:package createtextfiletest; public class CreateTextFileTest { /** * @param args the command line arguments */ public static void main(String[] args) { CreateTextFile app =new CreateTextFile(); app.OpenFile(); app.AddRecord(); app.CloseFile(); } }
-
Re: Create a Text file with java.
You might want to reply to answers to your previous question in this forum, as doing this shows that you're taking the time to read the replies of others and often motivates folks to help you further.
Similar Threads
-
Conversion of any image file to text file in Java...'
By suyog53 in forum New To JavaReplies: 12Last Post: 09-23-2012, 03:36 PM -
Read a data from a text file and create an object from these data in this text file
By jjavaa in forum Advanced JavaReplies: 2Last Post: 03-25-2011, 02:36 PM -
How to create an exe file in java
By pads in forum New To JavaReplies: 4Last Post: 07-09-2010, 05:20 PM -
How to create a hyperlink from a text in Java
By javanewbie in forum New To JavaReplies: 2Last Post: 06-05-2009, 02:12 PM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks