• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

a simple error

i read a file to an integer array but when i want to write it

like this;

// here i read "b.raw" file

try {
                        InputStream is = new FileInputStream("b.raw");
                        byte[] bDizi = new byte[640];
                        int okunanByteSayisi ;
                        while( (okunanByteSayisi = is.read(bDizi)) != -1 ) {
                              for(int i=0; i<okunanByteSayisi; i++) {
            int      so = bDizi[i];
                                    System.out.println(bDizi[i]  ); // here bDizi doesnt resolved
                              }

                        }
                        is.close();// baglantiyi kapat

                  } catch(IOException e) {
                        System.err.println(e);
                  }



// here i write a.raw file but the bDizi cannat be resolved.


Why??
0
dervisakyuz
Asked:
dervisakyuz
  • 5
  • 5
1 Solution
 
dervisakyuzAuthor Commented:
try {
                    InputStream is = new FileInputStream("b.raw");
                    byte[] bDizi = new byte[640];
                    int okunanByteSayisi ;
                    while( (okunanByteSayisi = is.read(bDizi)) != -1 ) {
                         for(int i=0; i<okunanByteSayisi; i++) {
          int     so = bDizi[i];
                              System.out.println(bDizi[i]  ); //not here
                         }

                    }
                    is.close();// baglantiyi kapat

               } catch(IOException e) {
                    System.err.println(e);
               }


try {
                                     
                                     int okunanByteSayisi ;
                                     fos = new FileOutputStream("b.raw"); // dosyaya baglan
                                     for( int i=0;i<okunanByteSayisi;i++) {
                                           
                                           
                                           fos.write(bDizi); // here  bDizi cannot resolved
                                     }
                               } catch (IOException exIo) {
                                     System.err.println("Hata Olustu:"+exIo);
                               } finally {
                                        try {
                                           fos.close(); // baglantiyi kapa
                                     } catch (IOException exClose) {
                                           System.err.println("Kapatmada sorun var:"+exClose);
                                     }  
                               }  
                           
                           
                   }
                   
                  
0
 
hoomanvCommented:
InputStream is = new FileInputStream("b.raw");
fos = new FileOutputStream("b.raw");

why are you reading and writting to the same file ?
you told that the output would be written to "a.raw"
0
 
hoomanvCommented:
also the write portion of your code does not correspond with the read part

// read
// you are reading the entire file contents, each time filling the array with new data discarding the previous read data

      byte[] bDizi = new byte[640];
      int okunanByteSayisi ;
      while( (okunanByteSayisi = is.read(bDizi)) != -1 ) {
            for(int i=0; i<okunanByteSayisi; i++) {
                  int so = bDizi[i];
                  System.out.println(bDizi[i]);
            }
      }

// write
// you are writing only the last read array, okunanByteSayisi times

      for( int i=0;  i<okunanByteSayisi; i++) {
            fos.write(bDizi);
      }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dervisakyuzAuthor Commented:
i want to copy b.raw to a.raw but read&#351;ng and again writing them

could u help me?
0
 
hoomanvCommented:
do you need only read b.raw and copy it exactly to a.raw ?
0
 
dervisakyuzAuthor Commented:
i mean that both using reading and writing i want to copy b.raw to a.raw.

pls help me
0
 
dervisakyuzAuthor Commented:
yes but a.raw not exist, we will write(create) it.

0
 
hoomanvCommented:
> but a.raw not exist, we will write(create) it.
no problem, openning FileOutputStream("a.raw") will create it

look into these examples
http://www.rgagnon.com/javadetails/java-0064.html
http://javaalmanac.com/egs/java.io/CopyFile.html
0
 
dervisakyuzAuthor Commented:
is there any way to copy exactly  not same.

some missing bytes etc. i need sth like this.

the file i have copied is a sound file.

i will show "a.raw" to  my teacher as  here is the echo cancelled file. :)



0
 
hoomanvCommented:
you need read the entire file,parse it, make the changes in memory, and then write it to output.
you should be familiar with sound file structure
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now