I'm working on a program that calls for me to write a program that opens a binary file and prints all ASCII characters from that file, that is, all bytes with values between 32 and 126. Print a new line after every 64 characters.

Here is a sample program run:
Filename:
HelloPrinter.class
2ā€¯HelloPrinterjava/lang/Object<init>()VCodeLineNumberTableLocalV
ariableTablethisLHelloPrinter;main([Ljava/lang/String;)Vjava/lan
g/SystemoutLjava/io/PrintStream;Hello, World!java/io/PrintStream
println(Ljava/lang/String;)Vargs[Ljava/lang/String;SourceFileHel
loPrinter.java!/*7 !

Here is my code

import java.io.InputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Scanner;



public class ASCIIPrinter
{

public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

System.out.println("Filename: ");

String filename = in.next();


try
{

InputStream inStream = new FileInputStream(filename);

boolean done = false;

int counter = 0;
final int LINE_LENGTH = 64;

while (!done)

{

// Write code here

}
if (counter > 0)

System.out.println();

inStream.close();

}
catch(IOException e)

{
e.printStackTrace();

}
}
}

Trying to figure out how I am suppose to use the input stream to read the text from a file and then output all ASCII characters (bytes between 32 and 126) from the file.

If you use the read() method, you can test every byte that is read to see if its one that you want to print.

Please wrap your code in code tags. Use the [code] icon above the input box.

Ok the read method from the InputStream class? Here is what I wrote so far. What exactly am I suppose to write to finish the line because I keep getting errors.

import java.io.InputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Scanner;



public class ASCIIPrinter
{

public static void main(String[] args)
{

Scanner in = new Scanner(System.in);

System.out.println("Filename: ");

String filename = in.next();


try
{

InputStream inStream = new FileInputStream(filename);

boolean done = false;

int counter = 0;
final int LINE_LENGTH = 64;

while (!done)

{
	
    InputStream in = ...///more code here
	int next = in.read();
	byte b;
	if (next != -1)
	   b = (byte) next;


}
if (counter > 0)

System.out.println();

inStream.close();

}
catch(IOException e)

{
e.printStackTrace();

}
}

I keep getting errors.

Please post the full text of the error messages

write to finish the line

Print a newline character: "\n" or call println() to end the current line of output

You can get a pointer to the beginning of the string and loop until you find the terminator. Count each position to get the string length. Use that length to determine the number of positions you will need to multiply each value in the string. As you loop again through each position, subtract 48 from each character and you will receive a numeric value to apply against the loop.

"123" becomes '1','2','3' becomes 1,2,3 becomes (1*100) + (2*10) + (3*1)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.