PDA

Click to See Complete Forum and Search --> : few question on java


leeckeat
Mar 8th, 2001, 09:48 AM
Q1. There have 2 classes, 1st is CreateText.class and 2nd is ReportApp. How can I call the CreateText main function to work on ReportApp ( i already import the class)?

Q2. When i create a text file using outstream method, why the first line have few unrecognized character inside? Can i delete it?

Q3.How to align int and double figure when display on screen/file? and how to set the decimal .00 rather than .0?

Hope that u guys can help me,
TQ!

Mar 8th, 2001, 04:42 PM
1. If you want to call the CreateText main method from the ReportApp class, use a call from within ReportApp which passes the correct argument (a String array):

If you normally pass in some strings on the command line to launch CreateText, then you need that String[] for the call.
String[] mayOrMayNotWantToFillThis = theStringArrayYouNormallyUse;
CreateText.main(mayOrMayNotWantToFillThis);

If you don't launch CreateText with arguments, this will work:
CreateText.main(new String[1]);

2. I'd have to see what you are trying to do. You would most likely corrupt the file if you deleted and characters within the file.

See http://java.sun.com/docs/books/tutorial/essential/io/
http://java.sun.com/docs/books/tutorial/essential/io/filestreams.html

3. See the DecimalFormat class at http://java.sun.com/docs/books/tutorial/java/data/numberintro.html and use the rightward arrow at the top-left and bottom-left of the page to navigate through the tutorial to get to http://java.sun.com/docs/books/tutorial/java/data/decimalFormat.html

leeckeat
Mar 11th, 2001, 12:16 AM
Regarding to create text file problem, here is the output of the text file (using word pad):

’ t
this is line 1
this is line 2

note:there have some unrecognise simbol in first line.

How do i to prevent those simbol appear on the text file?

Below is my java code:

import java.lang.System;
import java.io.*;

public class CreateText {
public static void main(String argv[]) throws IOException {
File file = new File("C:\\report.txt");
FileOutputStream outFile = new FileOutputStream(file);
ObjectOutputStream outStream = new ObjectOutputStream(outFile);

String line1 ="\n this is line 1";
String line2 ="\n this is line 2";

Object dataToSend = line1 + line2;
outStream.writeObject(dataToSend);

outStream.close();
outFile.close();
}
}

Mar 12th, 2001, 04:15 PM
If your intent is to serialize an object to disk (using the writeObject method) , then you should leave that data file alone, it needs that info to deserialize it.

But if you want to create a simple text file, you should see the tutorial I mentioned.

I'm sure that you want to use the java.io.PrintWriter class. There is a plethora of io stuff.

The following is bad Exception handling form, but shows the PrintWriter.

After your run it, inspect the file "some.txt" that it creates in the same directory.