Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-15-2004, 06:00 AM   #1 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
getting java to read from a file.

Hi there, i want to know how to get java to read a file that i will call from unix command line, do something with it and the n output the results.

the furthest i have got in what you do is to put the class

import java.io.*;

in at the beginning.

I want to read in a file, assign each column in the line to a variable

i.e if i was using perl it would be

Code:
while (my $line = <>) {   // while there is input from file
chomp $line;              // take off trailing space
($a, $b, $c, $d) = split ("\t", $line);  // 4 things are split by tabs on each line, put them into these variables 

print "$a\t$c\n";   // print first and third col                      

}

Last edited by sde; 10-15-2004 at 07:49 AM.
philthee is offline   Reply With Quote
Old 10-15-2004, 09:31 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
You can do this a couple of different ways. I suggest you make friends with the API, as it contains all the objects you'll use to make this work.

The simplest way is to take the standard input (System.in), and wrap it around a BufferedReader. That way you can pipe in the contents of the file (java MyProgram < input.txt). You can then use a StringTokenizer to parse the contents of each line.

Here's a bit of code you can use to get yourself started:
Code:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while(line != null){
  StringTokenizer toks = new StringTokenizer(line);
  // Do stuff
  line = br.readLine();
}
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-15-2004, 10:26 AM   #3 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
thanks a lot,
i am away from my unix machine and java stuff for the weekend but i will crack on with that on Monday

Cheers
philthee is offline   Reply With Quote
Old 10-19-2004, 05:02 AM   #4 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
i have tried putting in the code you have given me but the IDE does not seem to like it at all.
I am using Netbeans and when i inserted that bit of code it just underlines it with squiggly red line.

i have put it here in the main loop
public static void main(String[] args) {

your code

}

why does it not like that?
philthee is offline   Reply With Quote
Old 10-19-2004, 12:58 PM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Well, that code is pretty much verbatim from a quick Java file I coded up in pico. Netbeans can be a bit finicky at times (I use it myself), and if you had a typo earlier in the file it could throw the whole thing off.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-27-2004, 07:57 AM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Oh, for future reference, StringTokenizer is in java.util, I believe, and that's probably why you had the red error lines.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-27-2004, 08:00 AM   #7 (permalink)
philthee
Registered User
 
Join Date: Oct 2004
Posts: 15
philthee is on a distinguished road
cheers i got that
philthee is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
File DownLoad in java Gopinath_java Java 1 06-19-2004 02:45 AM
does PHP talk to JAVA class file?? Keith PHP 2 06-18-2004 08:23 AM


All times are GMT -8. The time now is 06:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting