Files and Streams Question
Hi! Java is my first and only language; and I am very new to Java. This is my first somewhat knowledgeable attempt to get a question answered on a forum of any kind. I put a question in this forum about two weeks ago, but I lost it. So, I am very grateful for any comments that can improve my ability to get my questions answered. Thank you. Mike
Here is my problem:
Read File: I want to read pT.txt or plainText.txt file (that is encoded in Unicode) into a String variable (inputLine).
(I want to do some processing of the inputLine String and assign the processing results into outputLine.)
Write File: I want to write outputLine into aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
Here is my question:
What is the simplest or easiest code for the Read File and Write File instructions?
Thanks. Mike
[868 byte] By [
lite_bluea] at [2007-11-27 9:22:09]

http://java.sun.com/docs/books/tutorial/essential/io/~
> I put a question in this forum about two weeks ago, but I lost it.
If you click on your user name it will give you a listing of your most recent postins. Since you only have 3 posts, it shouldn't be hard to find.
As for you Java problem, Google for a Java IO tutorial. Sun has one on this site somewhere if you look. It gives you all the details you need. When you have had a look at it, tried some code and run into problems, come back.
Here is what I have.pT.txt is below FnS.java
import java.io.*;
import java.util.Scanner;
public class FnS
{
public static void main(String[] args) throws Exception
{
File f = new File("pT.txt");
Scanner scan = new Scanner(f);
String peek1, peek2, peek3, peek4, peek5, peek6;
boolean pk1 = false, pk2 = false, pk3 = false, pk4 = false, pk5 = false;
int count = 0;
while(scan.hasNext())// begin search
{
peek1 = scan.nextLine();
if (peek1.startsWith("One"))
{
pk1 = true;
peek2 = scan.nextLine();
if(pk1 && peek2.startsWith("Two"))
{
pk2 =true;
while(pk1 && pk2)
{
peek3 = scan.nextLine();
if(pk1 && pk2 && peek3.startsWith("Three"))
{
pk3 = true;
peek4 = scan.nextLine();
if(pk1 && pk2 && pk3 && peek4.startsWith("Four"))
{
pk4 = true;
while(pk1 && pk2 && pk3 && pk4)
{
peek5 = scan.nextLine();
if(pk1 && pk2 && pk3 && pk4 && peek5.startsWith("Five"))
{
pk5 = true;
peek6 = scan.nextLine();
if(pk1 && pk2 && pk3 && pk4 && pk5 && peek6.startsWith("Six"))
{
/*
* (I want to do some processing of the inputLine String and assign the processing results into outputLine.)
*Write File: I want to write outputLine into aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
*Here is my question:
* What is the simplest or easiest code for the Read File and Write File instructions?
*Thanks. Mike
*/
System.out.println("\n" + peek6 + "\n" + peek5 + "\n" + peek4 + "\n" + peek3);
count++;
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//if pk1 && pk2 && pk3 && pk4 && pk5 && peek6
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//else begin new search
}//if pk1 && pk2 && pk3 && pk4 && peek5
}//while pk1 && pk2 && pk3 && pk4
}// if pk1 && pk2 && pk3 && peek4
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//else begin new search
}//if pk1 && pk2 && peek3
}//while pk1 && pk2
}//if pk1 && peek2
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
}//else begin new search
}//if peek1
}//while hasNext
System.out.println("\nemail count = " + count);
}
}
From
Date
To
Subject
One
Two
Three
Four
Five
Six
From
Date
To
Subject
One
Two
Three
Four
Five
Six
From
Date
To
Subject
One
Two
Three
Four
Five
Six
Can you explain what you are trying to do. I suspect those massively nested if statements and while loops are unnecessary.
I am using a simple .txt test case file (pT.txt) that represents a real stream of email. The original .txt test case file is a copy and paste of real (personal and confidential) email. At this step of development, I want to read pT.txt (that is encoded in Unicode) into a String variable. The next step that I want to do is convert the Unicode plain text into an ASCII encoded String and write it to aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
Forget all that scanning. Have a look at java.io.BufferedReader.readLine() and java.io.PrintWriter().
ejpa at 2007-7-12 22:16:28 >

Why am I not entering the while loop?
import java.io.*;
//not yet: import java.util.Scanner;
public class FnS
{
public static void main(String[] args) throws Exception
{
File inFile = new File("pT.txt");
BufferedReader input = new BufferedReader (new FileReader (inFile));
//not yet: PrintWriter outFile = new PrintWriter (new BufferedWriter (new FileWriter ("outFile.txt")));
String peek1, peek2, peek3, peek4, peek5, peek6;
boolean pk1 = false, pk2 = false, pk3 = false, pk4 = false, pk5 = false;
int count = 0;
while(input.ready())// begin search
{
peek1 = input.readLine();
if (peek1.startsWith("One"))
{
pk1 = true;
peek2 = input.readLine();
if(pk1 && peek2.startsWith("Two"))
{
pk2 =true;
while(pk1 && pk2)
{
peek3 = input.readLine();
if(pk1 && pk2 && peek3.startsWith("Three"))
{
pk3 = true;
peek4 = input.readLine();
if(pk1 && pk2 && pk3 && peek4.startsWith("Four"))
{
pk4 = true;
while(pk1 && pk2 && pk3 && pk4)
{
peek5 = input.readLine();
if(pk1 && pk2 && pk3 && pk4 && peek5.startsWith("Five"))
{
pk5 = true;
peek6 = input.readLine();
if(pk1 && pk2 && pk3 && pk4 && pk5 && peek6.startsWith("Six"))
{
/*
* (I want to do some processing of the inputLine String and assign the processing results into outputLine.)
*Write File: I want to write outputLine into aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
*Here is my question:
* What is the simplest or easiest code for the Read File and Write File instructions?
*Thanks. Mike
*/
System.out.println("\n" + peek6 + "\n" + peek5 + "\n" + peek4 + "\n" + peek3);
count++;
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//if pk1 && pk2 && pk3 && pk4 && pk5 && peek6
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//else begin new search
}//if pk1 && pk2 && pk3 && pk4 && peek5
}//while pk1 && pk2 && pk3 && pk4
}// if pk1 && pk2 && pk3 && peek4
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
break;
}//else begin new search
}//if pk1 && pk2 && peek3
}//while pk1 && pk2
}//if pk1 && peek2
else
{
pk1 = false;
pk2 = false;
pk3 = false;
pk4 = false;
pk5 = false;
}//else begin new search
}//if peek1
}//while hasNext
System.out.println("\nemail count = " + count);
}
}
deletedMessage was edited by: petes1234
> Why am I not entering the while loop?
Which 'while' loop? There are several.
while(input.ready())// begin search
Get rid of this pointless test. Input from a file is always ready.
> peek1 = input.readLine();
At this point, and after every readLine(), you must test for null, and exit the loop and close the file if you get it.
> pk1 = true;
> peek2 = input.readLine();
See above.
> if(pk1 && peek2.startsWith("Two"))
Get rid of the pointless test on pk1. How can it not be true?
> while(pk1 && pk2)
What exactly does this condition represent?
> peek3 = input.readLine();
See above.
> if(pk1 && pk2 && peek3.startsWith("Three"))
Again, what exactly does pk1 && pk2 test?
> peek4 = input.readLine();
See above.
> if(pk1 && pk2 && pk3 &&
> pk3 && peek4.startsWith("Four"))
What does this condition represent?
> while(pk1 && pk2 && pk3 && pk4)
and this one?
> {
> peek5 = input.readLine();
See above.
> if(pk1 && pk2 && pk3 && pk4 && && pk4 && peek5.startsWith("Five"))
What does this increasingly absurd condition represent?
> peek6 = input.readLine();
See above.
> if(pk1 && pk2 && pk3 && pk4 && pk5 && && pk5 && peek6.startsWith("Six"))
This is getting positively demented. How can any of these booleans possibly be false?
> count++;
> pk1 = false;
> pk2 = false;
> pk3 = false;
> pk4 = false;
> pk5 = false;
> break;
You would be bettter of using 'break label;' for some 'label' here, but I'm not convinced that any of these conditions really means anything.
> }//if pk1 && pk2 && pk3 && pk4 && pk5 && && pk5 && peek6
> else
> {
> pk1 = false;
> pk2 = false;
> pk3 = false;
> pk4 = false;
> pk5 = false;
> break;
> }//else begin new search
> }//if pk1 && pk2 && pk3 && pk4 && peek5
> }//while pk1 && pk2 && pk3 && pk4
> }// if pk1 && pk2 && pk3 && peek4
> else
> {
> pk1 = false;
> pk2 = false;
> pk3 = false;
> pk4 = false;
> pk5 = false;
> break;
> }//else begin new search
> }//if pk1 && pk2 && peek3
> }//while pk1 && pk2
> }//if pk1 && peek2
> else
> {
> pk1 = false;
> pk2 = false;
> pk3 = false;
> pk4 = false;
> pk5 = false;
> }//else begin new search
and the same goes for all this unwinding code.
What you need to do here is specify your problem. All I can see here is a fair amount of semi-nonsensical code that looks like it's been written by a berserk threshing machine ;-)
ejpa at 2007-7-12 22:16:28 >

What is wrong with the while statement?
> What is wrong with the while statement?sorry, it's your whole code. Please rethink it; rewrite it.
> What is wrong with the while statement?
I've pointed out something wrong with every while statement in the above. I don't think any of it makes any sense at all. I would throw it all away and start again with a clear statement of the problem. You've been asked for this a number of times and you still haven't provided it. You're not going to survive indefinitely without thinking this through properly, and nobody can help you until you do.
ejpa at 2007-7-12 22:16:28 >
