Results 1 to 7 of 7
- 12-07-2010, 03:33 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
-
You could use perhaps a PrintWriter object that is initialized with your output file or a FileReader object, and then call println on the PrintWriter object just as you do for System.out (which also happens to be a PrintWriter object).
- 12-07-2010, 03:45 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
Yes I am using PrintWriter, actually I read a file which has single int in each line total 64 lin4, and I create two dimentional array [8][8], and each int represent a specific character. and it is time to display this as an image. however when I try to write those character I wrote them in one line. But I want to write them 8 character per line so it will look like an image. I am not sure if I explain my problem clearly.
-
So if you know how to use PrintWriter, it should be easy to solve this. What have you tried?
- 12-07-2010, 04:09 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
this is what I have tried;
Java Code:import java.io.*; import java.util.Scanner; public class Program6 { static final int ROWS = 8; static final int COLS = 8; public static void main(String[] args) throws IOException { Scanner inFile; PrintWriter outFile; inFile = new Scanner(new File("infile.txt")); outFile = new PrintWriter(new FileWriter("outfile.txt")); String pixel; //declare and instantiate an array int [][] picture = new int [ROWS] [COLS]; for (int row = 0; row<ROWS; row++) { for(int col = 0; col<COLS; col++) { picture[row][col] = inFile.nextInt(); } } for(int row = 0; row<ROWS; row ++) { for (int col = 0; col<COLS; col++) { if(picture[row][col]>=0 && picture[row][col]<=7) { pixel = " "; } else if(picture[row][col]>=8 && picture[row][col]<=15) { pixel = "."; } else if(picture[row][col]>=16 && picture[row][col]<=23) { pixel =","; } else if(picture[row][col]>=24 && picture[row][col]<=31) { pixel = "-"; } else if(picture[row][col]>=32 && picture[row][col]<=39) { pixel = "+"; } else if(picture[row][col]>=40 && picture[row][col]<=47) { pixel = "o"; } else if(picture[row][col]>=48 && picture[row][col]<=55) { pixel = "0"; } else { pixel = "X"; } [COLOR="Red"] for (int counter= 0; counter<9; counter++) outFile.print(pixel); [/COLOR] } } inFile.close(); outFile.close(); } }
- 12-07-2010, 04:10 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 35
- Rep Power
- 0
I couldn't figure out how to display those pixels 8 characters per line
-
get rid of the for loop here:
Java Code:for (int counter= 0; counter<9; counter++) outFile.print(pixel);
and instead use the nested for loops that you already have.
Java Code:for(int row = 0; row<ROWS; row ++) { for (int col = 0; col<COLS; col++) { if(picture[row][col]>=0 && picture[row][col]<=7) { pixel = " "; } else if(picture[row][col]>=8 && picture[row][col]<=15) { pixel = "."; } else if(picture[row][col]>=16 && picture[row][col]<=23) { pixel =","; } else if(picture[row][col]>=24 && picture[row][col]<=31) { pixel = "-"; } else if(picture[row][col]>=32 && picture[row][col]<=39) { pixel = "+"; } else if(picture[row][col]>=40 && picture[row][col]<=47) { pixel = "o"; } else if(picture[row][col]>=48 && picture[row][col]<=55) { pixel = "0"; } else { pixel = "X"; } outFile.print(pixel); // prints a pixel for each column } outFile.println(); // prints a new line for each row }
Similar Threads
-
writing a program to read a specific amount of lines from a file?
By welikedogs in forum New To JavaReplies: 4Last Post: 11-03-2010, 06:17 PM -
read a specific line in an input file
By sara12345 in forum Advanced JavaReplies: 7Last Post: 01-03-2010, 10:40 PM -
writing to specific line in text file
By mickmos in forum New To JavaReplies: 2Last Post: 04-18-2009, 01:01 PM -
Writing To A Specific Text File Line
By mokonji in forum New To JavaReplies: 1Last Post: 03-02-2009, 08:13 PM -
New line character
By mew in forum New To JavaReplies: 3Last Post: 12-14-2007, 06:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks