I am facing some problems in a project regarding to byte fetching from a file and storing. Problem in java bytes is they are number, instead of char. In file i have seen a byte is like a char... Also how to make bytes unsigned , because signed bytes creates alot problem. Thanx in Advance.

A byte is 8 bits, nothing more. You may chose to interpret that as a number, or as a character, but that's up to you. The simple read() method for input streams places those 8 bits as the least significant 8 bits of an int variable, where you can use them as a number or cast to char (although there are better read methods for text data that handle the conversion from different byte-oriented character sets to UniCode).
What exactly do you want to do with your bytes?

i am making a compression tool for my assignement. Thats why i need to carefully read bytes from a file... Still there is a little success. I am thinking to shift to c++ because things are a bit easy there for such softwares.

There is absolutely no problem reading and writing bytes in Java, it's just that there a tiny gap in your knowledge somewhere that is preventing you from seeing the solution. You can read/write arrays of bytes with a single method call, and manipulate the bytes as numeric or character or just plain bits. If you can show what you are trying to do, eg in pseudo-code, then we can point you in the right direction.

OK, I really can't bear the thought of you having to resort to C++
Here's a tiny runnable program that shows wrting all possible byte values to a file, then readingthem in and displaying them as signed values, unsigned values, and characters...

import java.io.*;

public class ByteIODemo {
// simple demo of reading/writing bytes as unsigned ints or chars

   public static void main(String[] args) {
      // define a file in the current temp directory
      File tempDir = new File(System.getProperty("java.io.tmpdir"));
      File byteFile = new File(tempDir, "bytes.data");

      // write all 256 possible bytes values to the file...
      try {
         FileOutputStream out = new FileOutputStream(byteFile);
         for (int i = 0; i<=255; i++) out.write(i);
         out.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

      // read all the bytes...
      byte[] theBytes = new byte[256]; // input buffer
      try {
         FileInputStream in = new FileInputStream(byteFile);
         in.read(theBytes);
         in.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

      // interpret each byte as unsigned value and character
      for (byte b : theBytes) {
         int unsigned = b & 0xff; // mask out unwanted sign bits
         char c = (char) b;
         System.out.println(b + " " + unsigned + " " + c);
      }

   }   

}

Edited 4 Years Ago by JamesCherrill

Thank you cherril. After couple of hours work i finally got the problem issue.. and solved it. I was trying to post message but it wasnt posting.

This question has already been answered. Start a new discussion instead.