Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Converting Bytes into Char

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting Bytes into Char

    I was trying to redo File Input/Output again because I sort of forgot and I ran into a problem.

     
    import java.io.*;
    import java.util.concurrent.TimeoutException;
     
    public class FileIO 
    {
    	public static void main(String args[])
    	{
    		FileInputStream inputStream = null;
    		try
    		{
    			inputStream = new FileInputStream("X:\\Users\\unCryptifier\\Documents\\Scores.txt");
    		}
    		catch (FileNotFoundException e) 
    		{
    			e.printStackTrace();
    		}
     
    		byte readingBytes[] = new byte[24];
    		char convertedBytes[] = new char[5];
    		String scores[] = new String[5];
     
    		int i = 0;
     
    		while (i != -1)
    		{
    			try
    			{
    				readingBytes[i] = (byte)(inputStream.read());
    			}
    			catch (IOException e) 
    			{
    				e.printStackTrace();
    			}
    			System.out.println((char)(i));
    		}
     
     
    	}
    }


    The file that I'm trying to open has the numbers 0 to 25 in it with no space or lines in between. When I run the program, nothing shows in my console. It's running but nothing shows up and when I terminate the program, the only output it shows is space. I don't get what the problem is.


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Converting Bytes into Char

    Quote Originally Posted by unCrypticSolutions View Post
    ...sort of forgot
    You do know that there is extensive on-line documentation, rignt? FileInputStream

    I mean, it's always OK to ask, but wouldn't it be more time-effective for you to check the docs?

    Anyhow...
    Quote Originally Posted by unCrypticSolutions
    ... ran into a problem...
    One problem is that the read() function returns an integer, not a byte or a character. When a program tries to read past the end of the file, read() returns -1. If the return value from read() is not -1, you can cast the integer value to a byte or char or whatever...

    So a program to read a file a character at a time could go like this:
            byte readingBytes[] = new byte[24];
            int inputValue = 0;
            int i = 0;
            while ((inputValue != -1) && (i < readingBytes.length))
            {
                // Try{} Catch() is a good idea here, but I'll simplify...
                inputValue = inputStream.read();
                if (inputValue >= 0)
                {
     
                    // Store inputValue in readingBytes[i] or whatever...
     
                    // For debugging:
                    // Make the program tell you exactly what it is seeing at each step!
                    System.out.printf("i = %d, inputValue = %d (0x%02x)\n",
                            i, inputValue, inputValue);
                    ++i;
                }
            }
            System.out.printf("Number of characters read = %d\n", i);

    Quote Originally Posted by unCrypticSolutions
    The file that I'm trying to open has the numbers 0 to 25 in it with no space or lines in between...
    Hmmm. The plot thickens.


    OK, suppose you are now able to read all of the characters in the file.

    How do you know where one number ends and another begins?

    I mean, suppose the file contains 12121.

    Is this 1, 2, 1, 2, 1?
    Or is it 1, 2, 1, 21?
    Or is it 1, 2, 12, 1?
    Or is it 12, 1, 21?
    .
    .
    .
    .
    Or what???


    Cheers!

    Z
    Last edited by Zaphod_b; July 31st, 2012 at 02:45 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    4
    My Mood
    Lurking
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Converting Bytes into Char

    Short -> Char provides a bigger selection of characters

Similar Threads

  1. analizing bytes of files for an AV
    By Nessa in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 7th, 2011, 06:38 PM
  2. converting int to char
    By surfbumb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 19th, 2011, 12:59 AM
  3. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 11:19 AM
  4. How to Get the size of a file in bytes
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 11:19 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 04:12 AM