• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

input file and output file

 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all..just want a simple example of how to read from text file manipulate format and place the info in another text file..i prefer intergers not strings..thanks

cheers

sam
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the format of the integers in your text file? For example: ints separated by spaces, commas, lineshifts? Ints as text, ints and text?

What manipulations do you wish to accomplish?
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
o.k..here is the example i'm trying from the bruce eckel book:
date time tempreture
09032004 22.43 298.30

have to be manuplated to be:
Line 1:The date on which the Summary file is generated in the format dd/mm/yyyy.
Line 2:The time at which the Summary file is generated in the format hh:mm and
using a 24 hour clock format for the hours.
Line 3:The date and time of the first data reading in the data file using the same
format as that given immediately above.
Line 4:The date and time of the last data reading in the data file using the same
format as that given immediately above.
Line 5:The number of valid data readings found in the data file � including the first and last
readings if they are valid.
Line 6:The number of invalid data readings found in the data file � including the first and last
readings if they are invalid.
Line 7:The minimum valid temperature value found in the data file.
Line 8:The maximum valid temperature value found in the data file.
Line 9:The average of all the valid temperature values found in the data file.

i know pretty much how started by identifying the input and output files,creating text files, setup a tokenizer and place a try and catch statement, but i really don't how to put it all together..anyone can help..this not h/w but i have been given an assignment like that in which i need to practice and do examples like those to work it out.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to the I/O and Streams forum...
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone help on this please

thanks
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried to start it but after reading the i/o chapters and checking all examples from 3 different books..still can't figure it out..this is the only thing that i came up with:

import java.io.*;
import tempitem;
import java.util.StringTokenizer;
class temp

public static void main(string args[])

throws IOException {

DataInputStream datain;

DataInputStream dataout;
FileInputStream fin;
FileOutputStream fout;
int date;
finale float MAX = 350;
finle float MIN = 100;
float times, temperatures;


try {

dataout = new
DataOutputStream( new FileOutputStream ("summary.txt")



}

catch (IOException exc) {

System.out.println("Cannot open file.")
return;

}






}
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, don't use DataInput/OutputStreams. They're for binary data I/O, not for text. Use FileReader together with BufferedReader (for input) and PrintWriter (for output.)

Second, the best way to write a big program is to write a small one and add to it a little at a time, testing as you go. You've written a whole lot of code here which can't compile. You ought to start by getting just an empty class declaration to compile, and then an empty class including a main() to compile, and then have main print "hello, world" and run the program and make sure it works. Then try opening a file, reading a line, and printing it to System.out. Then you could open a second file for writing and putting the line into that file. Take small steps, and test each step along the way.

Third, write small methods. Don't write this whole program in main.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch djsami!

Not far from here there is an example of how to read a text file.

Regarding the parsing you can use a class like java.text.SimpleDateFormat:


Though the API recommends obtaining the Format derived instances from static methods like DateFormat.getTimeInstance/getDateInstance/getDateTimeInstance();
 
djsami mukhtar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but Ernest all data that i have read and output are binary e.g. date and temp..so i'm really confused why i have to use fileinput instead of datainput
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Binary" in the context of Java I/O usually refers to the format used by DataInputStream and DataOutputStream. To determine whether or not this is what you want, try printing out a few variables with a DataOutputStream (ie, using DataOutputStream myOutput = new DataOutputStream(System.out) , and see if you want to read data in that format. You'll probably find you don't.

In contrast, FileReader and PrintWriter read/write data in human-readable format. These, as Ernest suggests, are probably what you want.

As is often the case, the best way to determine if something is what you want is to try it out


--Tim
 
sam mukhtari
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim and Ernest, i have followed your lead and tried to write the program step by step, how ever when i wrote the read part sepertly and then the print write seperatly i can't seem to connect the two and don't know where the manipulation part of the text should be at..here it is so far:

import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;

class temp

public static void main(string args[])

throws IOException {

File f = new File(args[0]);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
PrintWriter pw = new PrintWriter( new FileWriter( "summary.txt" ) );
int date;
finale float MAX = 350;
finle float MIN = 100;
float times, temperatures;



void readMyFile() {

DataInputStream dis = null;
String record = null;
int recCount = 0;

try {

File f = new File("mydata.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

while ( (record=dis.readLine()) != null ) {
recCount++;
System.out.println(recCount + ": " + record);
}

} catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error!" + e.getMessage());

} finally {

if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
}
}


}
}
public class PrintWrite

{
public static void main( String[] args )
{
PrintWriter out = new PrintWriter( new FileWriter( "summary.txt" ) );
for

( int i=0 ; i <= 100 ; i++ )

out.close();
}

}






SimpleDateFormat sdf1 = new SimpleDateFormat("ddmmyyyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("dd/mm/yyyy");
Date date = sdf1.parse("09032004");System.out.println(sdf2.format(date));




}
 
Joe Ess
Bartender
Posts: 9280
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sam mukhtari:
how ever when i wrote the read part sepertly and then the print write seperatly i can't seem to connect the two

How about not doing the read and write seperately? Maybe putting a read and write in the same loop?


and don't know where the manipulation part of the text should be at

Probably between reading a record and writing a record.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic