473,327 Members | 2,016 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,327 software developers and data experts.

output file - BufferedWriter

281 100+
Good day!
Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
Must be something wrong about my codes..help me to figure it out, Thank you very much...
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < 5; i++)    {        
  2. try { 
  3.     output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true)); 
  4.     output.write(name+ " " + add+ " " + phone + "\n"); 
  5.     output.close(); 
  6.      }catch (IOException e) 
  7.       {
  8.        System.out.println("There was a problem creating/writing to the temp file");
  9.         e.printStackTrace();
  10.       } 
  11.  }
Apr 30 '07 #1
17 6326
r035198x
13,262 8TB
Good day!
Kindly please someone tells me why my program generates same content of output file? They're supposed to have different contents.
Must be something wrong about my codes..help me to figure it out, Thank you very much...
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i < 5; i++) { 
  2. try { 
  3. output = new BufferedWriter( new FileWriter(FILENAME + ind + ".txt", true)); 
  4. output.write(name+ " " + add+ " " + phone + "\n"); 
  5. output.close(); 
  6. }catch (IOException e) 
  7. {
  8. System.out.println("There was a problem creating/writing to the temp file");
  9. e.printStackTrace();
  10. }
What have you done in your code to make your output different?
Apr 30 '07 #2
shana07
281 100+
What have you done in your code to make your output different?
I put output.close(); outside try..
still the same...
Apr 30 '07 #3
r035198x
13,262 8TB
I put output.close(); outside try..
still the same...
Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.
Apr 30 '07 #4
shana07
281 100+
Shana, you are using a for loop to output to the file and the part of code you've posted has nothing to suggest that the output should be different.
Ok r035198x..hope you can leave this message for a while.
I need to solve this first, please ...
Expand|Select|Wrap|Line Numbers
  1. output.write(number + "\n");           
  2. output.write ("A:" +name);    
prints output:
51 A
How to make it go to next line like this:
51
A
Apr 30 '07 #5
JosAH
11,448 Expert 8TB
Ok r035198x..hope you can leave this message for a while.
I need to solve this first, please ...
Expand|Select|Wrap|Line Numbers
  1. output.write(number + "\n");           
  2. output.write ("A:" +name);    
prints output:
51 A
How to make it go to next line like this:
51
A
Wrap a PrintWriter around your BufferedWriter and use two separate println()s
for the fields.

kind regards,

Jos
Apr 30 '07 #6
shana07
281 100+
Wrap a PrintWriter around your BufferedWriter and use two separate println()s
for the fields.

kind regards,

Jos
Got it Josah!
Thank you very much.
Apr 30 '07 #7
shana07
281 100+
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you.
Expand|Select|Wrap|Line Numbers
  1.  for(int ind=0; ind < 10; ind += 2)  
  2.         {
  3.              b1 = arrayArgs[ind];
  4.              b2 = arrayArgs[ind+1];         
  5.  
  6.             switch (opcode) 
  7.              {           
  8.                 case GCD:       out = b1.gcd(b2).toString();
  9.                 break;
  10.                 default:        throw new BigAlException("invalid operation");
  11.              }  
  12.        String[] output = {out}; //this part - how to initiliaze out to an output array?  
  13.      }
  14.        System.out.println(output); //so here I can print all of outputs from the array
  15.  
Apr 30 '07 #8
JosAH
11,448 Expert 8TB
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you.
Expand|Select|Wrap|Line Numbers
  1.  for(int ind=0; ind < 10; ind += 2)  
  2.         {
  3.              b1 = arrayArgs[ind];
  4.              b2 = arrayArgs[ind+1];         
  5.  
  6.             switch (opcode) 
  7.              {           
  8.                 case GCD:       out = b1.gcd(b2).toString();
  9.                 break;
  10.                 default:        throw new BigAlException("invalid operation");
  11.              }  
  12.        String[] output = {out}; //this part - how to initiliaze out to an output array?  
  13.      }
  14.        System.out.println(output); //so here I can print all of outputs from the array
  15.  
"out" is an int (I guess), so your array initialization should look like this:
Expand|Select|Wrap|Line Numbers
  1. String output= { ""+out };
kind regards,

Jos
May 1 '07 #9
JosAH
11,448 Expert 8TB
Allow me to ask one more question about array syntax please.. please
take a look at codes below: Thank you.
Expand|Select|Wrap|Line Numbers
  1.  for(int ind=0; ind < 10; ind += 2)  
  2.         {
  3.              b1 = arrayArgs[ind];
  4.              b2 = arrayArgs[ind+1];         
  5.  
  6.             switch (opcode) 
  7.              {           
  8.                 case GCD:       out = b1.gcd(b2).toString();
  9.                 break;
  10.                 default:        throw new BigAlException("invalid operation");
  11.              }  
  12.        String[] output = {out}; //this part - how to initiliaze out to an output array?  
  13.      }
  14.        System.out.println(output); //so here I can print all of outputs from the array
  15.  
"out" is an int (I guess), so your array initialization should look like this:
Expand|Select|Wrap|Line Numbers
  1. String output= { ""+out };
The expression ""+out has type String so it can be an element of a String array.

kind regards,

Jos
May 1 '07 #10
shana07
281 100+
I am stucked now on how to separate my txt file content. I need to print out them using BufferedWriter method....

I have these data in a txt file:

2 0 groupAStart
2 1 groupAStart //From here
3 2 james
5 3 jun
3 4 groupAfinish
8 5 groupAfinish //Until here - goes to output1.txt
3 6 junk
5 7 junk
2 8 groupAStart //from here
2 9 groupAStart
3 10 jello
5 11 juniady
2 12 groupAfinish
2 13 groupAfinish // Until here - goes to output2.txt
.....
.....
....
I don't know what is the syntax to make it work. As I have done this........please advise me..Thank you.
Expand|Select|Wrap|Line Numbers
  1.  for(int i=0; i<=100 ; i++)  
  2. {           
  3.   try
  4.   { 
  5.     BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
  6.  
  7.   for(int ind=0; ind<=159451;ind++)
  8.    {
  9.                 String[] thelineFRST = lineFRST.split(" ");
  10.                 String pos = thelineFRST[0];
  11.                 String num = thelineFRST[1];
  12.                 String group = thelineFRST[2];                      
  13.  
  14.               if(method.equals(""))  //I am clueless this part
  15.               {
  16. bufferWriter = new BufferedWriter( new FileWriter("OutputFirst" +i + ".txt", true)); 
  17.                bufferWriter.write(pos+ " " +num+ " " + "\n");                  
  18.                bufferWriter.close(); 
  19.               }
  20.           }          
  21.          printWriter.close();   
  22.          inputFIRST.close();              
  23.     ....
  24.  
Happy Labour Day!
May 1 '07 #11
shana07
281 100+
"out" is an int (I guess), so your array initialization should look like this:
Expand|Select|Wrap|Line Numbers
  1. String output= { ""+out };
The expression ""+out has type String so it can be an element of a String array.

kind regards,

Jos
Yes, out is an int (BigInteger), and don't you think that it should look like this
Expand|Select|Wrap|Line Numbers
  1. String[] output= { ""+out };
But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.
May 1 '07 #12
JosAH
11,448 Expert 8TB
Yes, out is an int (BigInteger), and don't you think that it should look like this
Expand|Select|Wrap|Line Numbers
  1. String[] output= { ""+out };
But then, I still print nothing ....erm do you think it's good idea to initiliaze them in into array? Because my idea is to print all the output one short after finish calculating many input values.
My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
You want a String array so it can be constructed as you showed above.

This gives you an array with one element in it which is the String representation
of variable 'out'. I apologize for the typo.

kind regards,

Jos
May 1 '07 #13
shana07
281 100+
My bad, I mistyped my answer (lame excuse: I haven't had my espresso yet).
You want a String array so it can be constructed as you showed above.

This gives you an array with one element in it which is the String representation
of variable 'out'. I apologize for the typo.

kind regards,

Jos
hihiih..no worries ! I know that was a trick to test me ;)
I'll try again to make it works
Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...
May 1 '07 #14
JosAH
11,448 Expert 8TB
Kindly please read my aboved message...I have problem on how to separate file contents.Thanks a bunch...
Basically you have lines; each line contains three strings; when a line contains
a string "groupAStart" you want to start spooling the rest of the text to an
OutputStream or Writer until a line contains the String groupAEnd; right?
Something like this will get you started:
Expand|Select|Wrap|Line Numbers
  1. public void spool(BufferedReader in) throws IOException { // read from this reader
  2.    int fileno= 1; // file number in "output#.txt"
  3.    String line;
  4.    PrintWriter out= null; // if null: don't print anything
  5.    while ((line= in.readLine()) != null) { // while next line available
  6.       if (line.indexOf("groupAStart") >= 0) {
  7.          if (out == null)
  8.             out= makeOutFile(fileno++); // build a PrintWriter
  9.       }
  10.       else if (out != null) {
  11.          out.println(line);
  12.          if (line.indexOf("groupAEnd") >= 0) {
  13.             out.close();
  14.             out= null;
  15.          }
  16.    }
  17.    // let caller close the in stream
  18. }
Note that this little methods needs a couple of other methods, e.g. opening
a PrintWriter for the output given a serial number and it needs a 'driver' method
that opens the BufferedReader, calls this method and closes the reader again.
Instead of sticking all functionality in one big, ugly method better decompose
the problem like I did in this little example.

kind regards,

Jos
May 1 '07 #15
shana07
281 100+
Phew..I've typed wrong in my program.
just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
kindly please help me to figure it out ....really appreciate any help. thank you
Expand|Select|Wrap|Line Numbers
  1. public class Transfer
  2. {    
  3.      private static String lineFRST = "";
  4.  
  5.      public static void main(String[] args) throws IOException
  6.     {           
  7.          BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
  8.          spool(inputFIRST);
  9.     }
  10.  
  11.  public static void spool(BufferedReader in) throws IOException 
  12.  { 
  13.  BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));       
  14.  
  15.        PrintWriter printWriter = null;    
  16.        BufferedWriter bufferWriter = null;
  17.  
  18.        int fileno = 1;  
  19.        lineFRST = inputFIRST.readLine();   
  20.  
  21.        while(lineFRST != null) 
  22.        {
  23.          if (lineFRST.indexOf("groupAStart") >= 0)
  24.          {
  25.              if (printWriter == null)
  26.              makeOutFile (fileno++);
  27.          }
  28.          else if (printWriter != null)
  29.          {
  30.              printWriter.println(lineFRST);  
  31.              if (lineFRST.indexOf("groupAFinish") >=0)
  32.              {
  33.                  printWriter.close();
  34.                  printWriter = null;
  35.              }
  36.          }   
  37.        }
  38.  }
  39.  
  40.    public static void makeOutFile(int fileno) throws IOException 
  41.    {
  42.        PrintWriter printWriter = null;    
  43.        BufferedWriter bufferWriter = null;
  44.        bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true));   //this not works...     
  45.  
  46.         String[] thelineFRST = lineFRST.split(" ");
  47.         String num1 = thelineFRST[0];
  48.         String num2 = thelineFRST[1];
  49.         String method = thelineFRST[2];        
  50.  
  51.         bufferWriter.write(num1+ " " + num2+ "\n");                  
  52.         bufferWriter.close();            
  53.     }  
  54. }
May 1 '07 #16
JosAH
11,448 Expert 8TB
Phew..I've typed wrong in my program.
just finished deleted 27000 created output files in my dir. In the input file - there are 159451lines.
kindly please help me to figure it out ....really appreciate any help. thank you
Expand|Select|Wrap|Line Numbers
  1. public class Transfer
  2. {    
  3.      private static String lineFRST = "";
  4.  
  5.      public static void main(String[] args) throws IOException
  6.     {           
  7.          BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));
  8.          spool(inputFIRST);
  9.     }
  10.  
  11.  public static void spool(BufferedReader in) throws IOException 
  12.  { 
  13.  BufferedReader inputFIRST = new BufferedReader (new FileReader("first100.txt"));       
  14.  
  15.        PrintWriter printWriter = null;    
  16.        BufferedWriter bufferWriter = null;
  17.  
  18.        int fileno = 1;  
  19.        lineFRST = inputFIRST.readLine();   
  20.  
  21.        while(lineFRST != null) 
  22.        {
  23.          if (lineFRST.indexOf("groupAStart") >= 0)
  24.          {
  25.              if (printWriter == null)
  26.              makeOutFile (fileno++);
  27.          }
  28.          else if (printWriter != null)
  29.          {
  30.              printWriter.println(lineFRST);  
  31.              if (lineFRST.indexOf("groupAFinish") >=0)
  32.              {
  33.                  printWriter.close();
  34.                  printWriter = null;
  35.              }
  36.          }   
  37.        }
  38.  }
  39.  
  40.    public static void makeOutFile(int fileno) throws IOException 
  41.    {
  42.        PrintWriter printWriter = null;    
  43.        BufferedWriter bufferWriter = null;
  44.        bufferWriter = new BufferedWriter( new FileWriter("output" +fileno +".txt", true));   //this not works...     
  45.  
  46.         String[] thelineFRST = lineFRST.split(" ");
  47.         String num1 = thelineFRST[0];
  48.         String num2 = thelineFRST[1];
  49.         String method = thelineFRST[2];        
  50.  
  51.         bufferWriter.write(num1+ " " + num2+ "\n");                  
  52.         bufferWriter.close();            
  53.     }  
  54. }
I don't understand what you're doing here:

1) you create a BufferedReader in you main() method, you pass it to the spool()
method and there you ignore it and open another BufferedReader. Why?

2) I my suggested code the makeOutFile method was just supposed to create
and return a PrintWriter; you decided to make it a void method. Why?

3) You write something in the makeOutFile method. Why?

4) Note that the printWriter is always null because you never assign something
to that variable? Why not?

kind regards,

Jos
May 1 '07 #17
shana07
281 100+
I don't understand what you're doing here:

1) you create a BufferedReader in you main() method, you pass it to the spool()
method and there you ignore it and open another BufferedReader. Why?

2) I my suggested code the makeOutFile method was just supposed to create
and return a PrintWriter; you decided to make it a void method. Why?

3) You write something in the makeOutFile method. Why?

4) Note that the printWriter is always null because you never assign something
to that variable? Why not?

kind regards,

Jos
I managed to solve it Josah - by your advice of cause (after got so many Why Qs from you... :))
I am learning a lot from you.thank you very much
May 1 '07 #18

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

Similar topics

5
by: Konrad Den Ende | last post by:
I tried to write some Unicode-16 characters (that were displayed correctly, as expected, on the screen) to a file but it didn't work out very well. I have those in an char as well as a String. Both...
2
by: slukacs | last post by:
hello one and all, i am still new to java but becoming ever so more efficient. i need some advice and guidance on big strings. i am debugging a special counter that i created in java and i am...
1
by: Row | last post by:
Hi, I am having trouble writting from a JTextArea to a flat text file (locally) I have tried using string tokenizer to tokenize the string first but with no luck, Im stuck on the one :( im...
2
by: slickn_sly | last post by:
For some reason, I'm getting an error. It doesn't seem to be reading my "input.txt" file. I'm trying to read the "input.txt" file which consits of random integers and use insertion sort to sort...
9
by: asenthil | last post by:
Hai to all,, i had to tried to retrive and write a single row to a file from the database.. But dont know to write all the retrived rows to a file from the database.. how to do that... ...
1
by: PvtBillPilgrim | last post by:
I need to write a method called save that takes a String as a parameter, and returns a boolean value. The parameter represents a filename, and the method should save the contents of one of my classes...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
2
by: deeadeed | last post by:
Hey, Im working on a programme and need it to write to file. I put in a file writer it seems to run ok, creates the txt file but doesnt write any data. Heres the code: private void...
2
by: minouparhizkar | last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.