Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

how to write result in file.....

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I am parsing a text file using stream tokenizer.
on command line I invoke the class ( code is given below) as :
javac Tokenexample myfile.txt "*"
and it gives me correct output .. i.e delimits whole file based on Word, Character and Numbers.
My question is : I want to Write the output to a different file say another text file and put all Words together.. all Characters together... can anyone please tell me how ?
the code for Tokenizing and mytext.txt is follows,
many thanks
Ashish

-----------------------
import java.io.*;
public class Tokenexample {
public static void main(String args[]) throws FileNotFoundException,IOException {


FileReader file = new FileReader( args[0]);
BufferedReader in = new BufferedReader(file);
StreamTokenizer st = new StreamTokenizer(in);

char [] c = args[1].toCharArray();

for (int i=0; i < c.length; i++) {

System.out.println("Whitespace will include ' " + c[i] + "'" );
st.whitespaceChars(c[i] , c[i]);

}


int tokval;

while ((tokval = st.nextToken() ) != StreamTokenizer.TT_EOF ) {

switch (tokval) {

case StreamTokenizer.TT_WORD :

System.out.println("Word token \"" + st.sval + "\"");
break;

case StreamTokenizer.TT_NUMBER :

System.out.println("Number token \"" + st.nval + "\"");
break;

default :

System.out.println("Character '" + (char) tokval + "'");


}
}

}
}
-------------------------------
myfile.txt
This is written to a file
How can this be a file ?
123 is not a b c d

-----------------------
I modified the source to allow the complete for loop to show! Carl
[This message has been edited by Carl Trusiak (edited November 29, 2000).]
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , i am a beginner and did a program which stored
the output in a seperate txt. file, in my case the output was a
string named str[].
the concept was like this:
DataOutputStream out1 = new DataOutputStream(new FileOutputStream("text1.txt));
for (int i=0; i<str.length; i++)>
{
out1.writechars(str[i]);
}
out1.close();

I hope this helps u little bit, if this is not correct pls, let me know.
}
 
Ashish Agnihotri
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramesh,
thanks for your input. I am still bit confused. Can you please do me a favour... can you put that piece of info in my code and post it.. ( modified).
Will write you more after that.
tks,
Ash
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!