473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

File Writing

hi, im relatively new at programming.
Im using bluej.

i have manged to write the following data to a file.

0213 - Sumair Seth
9801 - John Smith
7373 - Jason Porter
1283 - Allison Fairth

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Jan 13 '07 #1
4 2745
r035198x
13,262 8TB
hi, im relatively new at programming.
Im using bluej.

i have manged to write the following data to a file.

0213 - Sumair Seth
9801 - John Smith
7373 - Jason Porter
1283 - Allison Fairth

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Have you written any code to read the file. Post the code you have written so far.
Jan 13 '07 #2
yes this is the code to enter the details into the file:

Expand|Select|Wrap|Line Numbers
  1.  import java.io.*;
  2. public class RegisterEmployees
  3. {
  4. static    InputStreamReader in=new InputStreamReader(System.in);
  5. static     BufferedReader br = new BufferedReader(in);
  6.     public static void enterEmployeeNames() throws IOException
  7.     {
  8.         File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
  9.  
  10.         BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
  11.         System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
  12.         int numberofUpdates = Integer.parseInt(br.readLine());
  13.         for(int i=1;i<=numberofUpdates ; i++)
  14.         {
  15.  
  16.         System.out.println("Enter four digit Employee Code");
  17.        String EmpCode = br.readLine();
  18.  
  19.        if(EmpCode.length()<4)
  20.        {
  21.         System.out.println("Code to small please begin again and enter a four digit code");
  22.     enterEmployeeNames();    
  23.     }
  24.     if(EmpCode.length()>4)
  25.        {
  26.         System.out.println("Code to large please begin again and enter a four digit code");
  27.     enterEmployeeNames();    
  28.     }   
  29.     out.write(EmpCode);
  30.  
  31.         out.write(" - ");
  32.         System.out.println("Enter Employee Name");
  33.         out.write(br.readLine());
  34.                 System.out.println("Thank You, Employee Database has been updated");
  35.     }
  36. out.write("\n");
  37.     out.close();     }
  38.  
  39.     }
  40.  


basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now
Jan 13 '07 #3
r035198x
13,262 8TB
yes this is the code to enter the details into the file:

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class RegisterEmployees
  3. {
  4. static InputStreamReader in=new InputStreamReader(System.in);
  5. static BufferedReader br = new BufferedReader(in);
  6. public static void enterEmployeeNames() throws IOException
  7. {
  8. File EmpDatabase = new File("C:\\Documents and Settings\\All Users\\Desktop");
  9.  
  10. BufferedWriter out = new BufferedWriter(new FileWriter("C:\\Documents and Settings\\All Users\\Desktop\\Database.txt", true));
  11. System.out.println("Kindly Enter Number of Employees whose records need to be Entered");
  12. int numberofUpdates = Integer.parseInt(br.readLine());
  13. for(int i=1;i<=numberofUpdates ; i++)
  14. {
  15.  
  16. System.out.println("Enter four digit Employee Code");
  17. String EmpCode = br.readLine();
  18.  
  19. if(EmpCode.length()<4)
  20. {
  21. System.out.println("Code to small please begin again and enter a four digit code");
  22. enterEmployeeNames(); 
  23. }
  24. if(EmpCode.length()>4)
  25. {
  26. System.out.println("Code to large please begin again and enter a four digit code");
  27. enterEmployeeNames(); 
  28. out.write(EmpCode);
  29.  
  30. out.write(" - ");
  31. System.out.println("Enter Employee Name");
  32. out.write(br.readLine());
  33. System.out.println("Thank You, Employee Database has been updated");
  34. }
  35. out.write("\n");
  36. out.close(); }
  37.  
  38. }
  39.  


basically the number will always be 4 digits, to make reading it easier. no i havent written any code to read the file as of now
Write the part for reading the file post that. You will need to use
Expand|Select|Wrap|Line Numbers
  1.  
  2. BufferedReader br = new BufferedReader(new FileReader(String nameOfFile));
  3.  
Then you read a line using
Expand|Select|Wrap|Line Numbers
  1. br.readLine();
Jan 13 '07 #4
horace1
1,510 Expert 1GB

I need to read only the 4 digit string(0213 and 9801) and write it into an array. I need to do this everytime i write a number and a name(in the above format) to the file. Could someone help me with this?... i really need it urgently.
Are you talking about another program which is reading this data or do you need to create an array within this program to hold the employer code?
In the latter case when you write the employer code
Expand|Select|Wrap|Line Numbers
  1.     out.write(EmpCode);
  2.  
you could store the value in a vector, e.g.
Expand|Select|Wrap|Line Numbers
  1.     Vector<Integer> v = new Vector<Integer>();
  2.    ....
  3.     out.write(EmpCode);
  4.     Integer i=new Integer(EmpCode);
  5.     v.add(i);
  6.  
Jan 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
2
by: Pierre Rouleau | last post by:
Hi all! I'm trying to extend the functionality of the file object by creating a class that derives from file. MyFile class re-implements __init__(), write(), writelines() and close() to augment...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
12
by: glennanthonyb | last post by:
Hi The company I work for has finally woken up to data security on our field laptops. I'm writing something in C# that will allow remote deletion of sensitive data and I don't believe...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.