|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Oct 2009
Messaggi: 72
|
[Java] suggerimento su RandomAccessFile
Codice:
String str = ""; byte data[] = null; RandomAccessFile f = new RandomAccessFile(new File("U://test.txt"), "rw"); f.setLength(0) ; for (...) { str = ...//recupera la stringa f.setLength(0) ; data = str.getBytes(); f.getChannel().position(0); f.write(data); } f.close(); ho un codice così scritto; recupera delle stringhe da delle code MQ e le scrive su file i requisiti sono 1) ogni stringa del ciclo for sovrascrive nel file quella precedente 2) se il processo cade (un system.exit per intenderci), sul file deve rimanere l'ultima stringa la domanda: non sono espertissimo di classi Java per la scrittura, è giusto utilizzare RandomAccessFile o mi consigliate qualcosa di diverso? In termini di performance principalmente Grazie |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Oct 2009
Messaggi: 72
|
Codice:
String newStr=""; String filePath= "U://test.txt"; FileInputStream fis = new FileInputStream(filePath); String content = IOUtils.toString(fis); FileOutputStream fos = new FileOutputStream(filePath); IOUtils.write(content, new FileOutputStream(filePath)); for (...) { newStr=...//recupera la stringa content = content.replaceAll(content, newStr); fos = new FileOutputStream(filePath); IOUtils.write(content, new FileOutputStream(filePath)); } fis.close(); fos.close(); meglio? Grazie |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 12:30.