| | |
How do you output to the screen?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2003
Posts: 4
Reputation:
Solved Threads: 0
Ok here is a program I am working on for an assignment.
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("p1.txt");
File outputFile = new File("p2.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
Now it works fine and everything, the problem is that I want it to print the outputFile to the screen when I run the application. And I can't figure out how! Our teacher would only say that it has something to do with a buffered reader, and I have never been very talented at those. :cry: If anyone could help with this I would really appreciate it.
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("p1.txt");
File outputFile = new File("p2.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
Now it works fine and everything, the problem is that I want it to print the outputFile to the screen when I run the application. And I can't figure out how! Our teacher would only say that it has something to do with a buffered reader, and I have never been very talented at those. :cry: If anyone could help with this I would really appreciate it.
•
•
Join Date: Sep 2003
Posts: 4
Reputation:
Solved Threads: 0
0
#4 Sep 14th, 2003
Well I figured that part out! I had to use a buffered reader. Now I am stuck again though. I have read the data and divided it up with substring(num,num) like it needs to be and trimmed of the extra space. I have a while loop doing this but I can't figure out how to make the while loop do the whole file. Right now I have it using a counter but that won't work for the whole thing.
here it is: any suggestions for what the while condition should be?
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.*;
public class Copy3 {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader( new FileReader( "fruits.dat" ));
StringBuffer buffer = new StringBuffer();
String text;
String complete;
String idstring;
String statestring;
String datestring;
String fruitstring;
String gradestring;
String coststring;
String otheroutput;
int counting;
counting = 1;
//otheroutput=input.readLine();
while (counting != 6) {
complete=input.readLine();
if (complete.substring(0,1).equals(" ") )
{
fruitstring=complete.substring(10,20);
fruitstring=fruitstring.trim();
gradestring=complete.substring(20,22);
gradestring=gradestring.trim();
coststring=complete.substring(22,30);
coststring=coststring.trim();
System.out.println(fruitstring);
System.out.println(gradestring);
System.out.println(coststring);
}
else
{
idstring=complete.substring(0,10);
statestring=complete.substring(10,12);
datestring=complete.substring(12,20);
System.out.println(idstring);
System.out.println(statestring);
System.out.println(datestring);
}
//otheroutput=input.readLine();
counting=counting + 1;
}
}
}
here it is: any suggestions for what the while condition should be?
import java.io.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
import java.awt.font.*;
import java.awt.*;
public class Copy3 {
public static void main(String[] args) throws IOException {
BufferedReader input = new BufferedReader( new FileReader( "fruits.dat" ));
StringBuffer buffer = new StringBuffer();
String text;
String complete;
String idstring;
String statestring;
String datestring;
String fruitstring;
String gradestring;
String coststring;
String otheroutput;
int counting;
counting = 1;
//otheroutput=input.readLine();
while (counting != 6) {
complete=input.readLine();
if (complete.substring(0,1).equals(" ") )
{
fruitstring=complete.substring(10,20);
fruitstring=fruitstring.trim();
gradestring=complete.substring(20,22);
gradestring=gradestring.trim();
coststring=complete.substring(22,30);
coststring=coststring.trim();
System.out.println(fruitstring);
System.out.println(gradestring);
System.out.println(coststring);
}
else
{
idstring=complete.substring(0,10);
statestring=complete.substring(10,12);
datestring=complete.substring(12,20);
System.out.println(idstring);
System.out.println(statestring);
System.out.println(datestring);
}
//otheroutput=input.readLine();
counting=counting + 1;
}
}
}
•
•
Join Date: Oct 2003
Posts: 1
Reputation:
Solved Threads: 0
I may be reading your question wrong, and I know you are close to getting it the other way, but this is how I would do it.
public void readFile()
{
String fileName = "INPUT_FILE_NAME", readLine;
// Will read until the end of the file, if it exists \\
try
{
BufferedReader in = new BufferedReader(
new FileReader( fileName ) );
readLine = in.readLine();
while ( readLine != null )
{
System.out.println( readLine );
readLine = in.readLine();
}
in.close();
}
// If file does not exist \\
catch ( IOException iox )
{
System.out.print("File Not Found");
}
}
public void readFile()
{
String fileName = "INPUT_FILE_NAME", readLine;
// Will read until the end of the file, if it exists \\
try
{
BufferedReader in = new BufferedReader(
new FileReader( fileName ) );
readLine = in.readLine();
while ( readLine != null )
{
System.out.println( readLine );
readLine = in.readLine();
}
in.close();
}
// If file does not exist \\
catch ( IOException iox )
{
System.out.print("File Not Found");
}
}
![]() |
Similar Threads
- How to display "\n" on output screen in C++? (C++)
- Flashing the output to the screen for a limited time (C++)
- Screen Output plus Logging in batch file (IT Professionals' Lounge)
- In which case "Null Pointer Assignment" will be displayed on the output ?? (C)
- Getting and displaying Output Debug messages to listbox (C#)
- dont know how to output this (C)
- I Summon Clear Screen (C++)
Other Threads in the Java Forum
- Previous Thread: Creating the piglatin version of a string
- Next Thread: User clicked no on a java applet from IBM
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






Someone suggested a try block?