Java How to write a string in file as bytes and get string back -


I have to type the string in a file as a byte in UTF-8 and then retrieve these bytes from the file And it has to change back to string and get the same string as an abstract. It may seem easy but there is a hidden problem such as incorrect symbols in the file. I mean that after adding it to the file it should be something like this:

00000008 d0bad0bb d18ed187 00000010 etc ...

But it contains content like this:

mystring --- many places --- (and the symbol that does not appear here)

So, what have I already done? I have tried this way:

Before reading the code: I keep the wire in the hashmap & lt; String, string & gt; So my code includes (...) etc.

  try {FileOutputStream oStream = new FileOutputStream ("filename.txt"); Set up & lt; String & gt; KeySet = storage.keySet (); Bytebuff buffer = Bytebuffer.Olocate (1024); For (string key: keyset) {byte [] keybank = keygate ("UTF-8"); Byte [] valueInByte = store.get (key) .getBytes ("UTF-8"); OStream.write (buffer.IPIIT (0, valueInByte.length) .re ()); OStream.write (keyInByte); Ostream.written ((buffer.infoInt (0, value inbeta.lampper) .night ())); OStream.write (valueInByte); }} Hold (exception e) {System.err.println ("Permission denied"); }   

I have tried to use printwire, firefighter, etc. ... but this is not what I need. For example, some of them require string () method, but after afterString () I'll lose the ability to work with bytes.

! Note, I have tried to convert my notepad into UTF-8 encode, but it does not get any result.

If you want to use a property then you can do this.

  New property (map). Save (new fileOutputStream ("filename.proeprties"));   

to load properties

  properties property = new property (); Prop.load (new FileInputStream ("filename.properties")); map.putAll (hop);   

You can use properties as a map to save a copy of the data. Note: A key can not contain any = or a new row.


How do I do this in a binary format

  DataOutputStream DOS = New BufferedOutputStream (New FileOutputStream ("filename.dat"))); Dos.writeInt (map.size ()); (Map. Entry & lt; string, string & gt; entry: map.entrySet ()) {dos.writeUTF (entry.getKey ()); Dos.writeUTF (entry.getValue ()); } Dos.close ();   

In this way I will write it in text using UTF-8 / P <> PrintWriter pw = new PrintWriter (New OutputStream Witter (New FileFootStream "Filename.txt) ")" UTF-8 ");) Pw.println (map.size ()); (Map. Entry & lt; string, string & gt; entry: map.entrySet ()) {pw.println (entry.getKey ()); Pw.println (entry.getValue ()); } Pw.close (); Public static string encoding (strings) {// If you can not accept a new line, do not do anything. Return s.replaceAll ("\\\\", "\\\\\\"). replaceAll ("\ n", "\\\\ n"); }

This will produce a similar kind

  key1 value1 key2 value2   

If you believe that the key does not contain = or : or tab, then you can use a property file like a line

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -