-1

I need to be able to find a certain value (such as 80) in a file like:

4D 54 68 64 00 00 00 06 00 01 00 01 00 C0 4D 54 72 6B 00 00 00 31 00 90 60 40 81 40 80 60 40 00 90 62 40 81 40 80 62 40 00 90 64 40 81 40 80 64 40 00 90 62 40 81 40 80 62 40 00 90 57 40 81 40 80 57 40 00 FF 2F 00

example of my code :

RandomAccessFile file = new RandomAccessFile(new File(fichierAConvertir), "rw");

        System.out.println(fichierAConvertir);
        if(file.read() == 0x80)
        {
            System.out.println("trouvé");
        }
        else{
            System.out.println("pas trouvé");
        }

I also want to be able to replace all cases of that value by an another value .

I'm French, so sorry for my English skills..

Clyx
  • 11
  • 5
  • 1
    You need me to find 80 in a file? I think you need to write some code yourself to find it. Show some effort and you will receive help. – Kallja Jan 27 '14 at 15:31
  • yes. I'm a begginer and i'm stuck about this i tried with regex but doesn't work.. i know how to do for non hex value . @Kallja – Clyx Jan 27 '14 at 15:34
  • Then post your code (only relevant bits, but complete and compiling) and possible stack traces and let's see what's wrong with it. The idea of this sit isn't provide complete solutions for problems but the help you find one for yourself. @Clyx – Kallja Jan 27 '14 at 15:39
  • if you don't want to use regex, try with the Scanner class, it's simpler for a beginner http://stackoverflow.com/questions/11871520/how-to-use-the-scanner-class-in-java – Alessio Jan 27 '14 at 15:43
  • I edit my post. @Kallja – Clyx Jan 27 '14 at 15:43
  • @Kalija It doesn't have to ***always*** compile if compiling is the issue that needs to be resolved. |=^/ – But I'm Not A Wrapper Class Jan 27 '14 at 15:43

1 Answers1

0

You could easily read the file into a String, then do a replace all on that String. Finally, write that updated String back into the file. You can read the file into a String (found here):

Scanner scanner = new Scanner(new File("filename"));
String content = scanner.useDelimiter("\\Z").next();
scanner.close();

There are alternative ways to have a File turn to a String in these two links:

Choose one of these 3 methods, then continue with the following code.

There is a replaceAll() method for String in the API:

content = content.replaceAll("80", "some other string");

Finally, write this update String back into the file as so:

PrintWriter writer = new PrintWriter("filename", "UTF-8");
writer.println(content);
writer.close();//don't forget to close!

If you want to be able to convert String to hex and back, then I'd check out this question.

NOTE: Don't forget to close your writer/scanner/printer/etc. Check out this solution as to why.

Community
  • 1
  • 1