Программисты JAVA общаются здесь
Anonymous
Лучший способ написать строку для файла, используя Java Nio
Сообщение
Anonymous » 03 май 2025, 23:56
Мне нужно написать (добавить) огромную строку в плоский файл, используя Java Nio. Кодирование ISO-8859-1. < /p>
В настоящее время мы пишем, как показано ниже. Есть ли какой -нибудь
лучше < /strong> способ сделать то же самое? < /P>
Код: Выделить всё
public void writeToFile(Long limit) throws IOException{
String fileName = "/xyz/test.txt";
File file = new File(fileName);
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = null;
String messageToWrite = null;
for(int i=1; i
Изменить: Пробое оба параметра. Ниже приведены результаты. < /P>
@Test
public void testWritingStringToFile() {
DiagnosticLogControlManagerImpl diagnosticLogControlManagerImpl = new DiagnosticLogControlManagerImpl();
try {
File file = diagnosticLogControlManagerImpl.createFile();
long startTime = System.currentTimeMillis();
writeToFileNIOWay(file);
//writeToFileIOWay(file);
long endTime = System.currentTimeMillis();
System.out.println("Total Time is " + (endTime - startTime));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @param limit
* Long
* @throws IOException
* IOException
*/
public void writeToFileNIOWay(File file) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
FileChannel fileChannel = fileOutputStream.getChannel();
ByteBuffer byteBuffer = null;
String messageToWrite = null;
for (int i = 1; i < 1000000; i++) {
messageToWrite = "This is a test üüüüüüööööö";
byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset
.forName("ISO-8859-1")));
fileChannel.write(byteBuffer);
}
}
/**
*
* @param limit
* Long
* @throws IOException
* IOException
*/
public void writeToFileIOWay(File file) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(file, true);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
fileOutputStream, 128 * 100);
String messageToWrite = null;
for (int i = 1; i < 1000000; i++) {
messageToWrite = "This is a test üüüüüüööööö";
bufferedOutputStream.write(messageToWrite.getBytes(Charset
.forName("ISO-8859-1")));
}
bufferedOutputStream.flush();
fileOutputStream.close();
}
private File createFile() throws IOException {
File file = new File(FILE_PATH + "test_sixth_one.txt");
file.createNewFile();
return file;
}
Использование Bytebuffer и Channel: занял 4402 мс [/b]
Использование буферизации: 563 мс
Подробнее здесь:
https://stackoverflow.com/questions/736 ... g-java-nio
1746305819
Anonymous
Мне нужно написать (добавить) огромную строку в плоский файл, используя Java Nio. Кодирование ISO-8859-1. < /p> В настоящее время мы пишем, как показано ниже. Есть ли какой -нибудь [b] лучше < /strong> способ сделать то же самое? < /P> [code]public void writeToFile(Long limit) throws IOException{ String fileName = "/xyz/test.txt"; File file = new File(fileName); FileOutputStream fileOutputStream = new FileOutputStream(file, true); FileChannel fileChannel = fileOutputStream.getChannel(); ByteBuffer byteBuffer = null; String messageToWrite = null; for(int i=1; i Изменить: Пробое оба параметра. Ниже приведены результаты. < /P> @Test public void testWritingStringToFile() { DiagnosticLogControlManagerImpl diagnosticLogControlManagerImpl = new DiagnosticLogControlManagerImpl(); try { File file = diagnosticLogControlManagerImpl.createFile(); long startTime = System.currentTimeMillis(); writeToFileNIOWay(file); //writeToFileIOWay(file); long endTime = System.currentTimeMillis(); System.out.println("Total Time is " + (endTime - startTime)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * * @param limit * Long * @throws IOException * IOException */ public void writeToFileNIOWay(File file) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(file, true); FileChannel fileChannel = fileOutputStream.getChannel(); ByteBuffer byteBuffer = null; String messageToWrite = null; for (int i = 1; i < 1000000; i++) { messageToWrite = "This is a test üüüüüüööööö"; byteBuffer = ByteBuffer.wrap(messageToWrite.getBytes(Charset .forName("ISO-8859-1"))); fileChannel.write(byteBuffer); } } /** * * @param limit * Long * @throws IOException * IOException */ public void writeToFileIOWay(File file) throws IOException { FileOutputStream fileOutputStream = new FileOutputStream(file, true); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( fileOutputStream, 128 * 100); String messageToWrite = null; for (int i = 1; i < 1000000; i++) { messageToWrite = "This is a test üüüüüüööööö"; bufferedOutputStream.write(messageToWrite.getBytes(Charset .forName("ISO-8859-1"))); } bufferedOutputStream.flush(); fileOutputStream.close(); } private File createFile() throws IOException { File file = new File(FILE_PATH + "test_sixth_one.txt"); file.createNewFile(); return file; } [/code] Использование Bytebuffer и Channel: занял 4402 мс [/b] [b] Использование буферизации: 563 мс [/b] Подробнее здесь: [url]https://stackoverflow.com/questions/7366266/best-way-to-write-string-to-file-using-java-nio[/url]
0 Ответы
30 Просмотры
Последнее сообщение Anonymous
14 май 2024, 20:40
0 Ответы
11 Просмотры
Последнее сообщение Anonymous
12 апр 2025, 16:10
0 Ответы
68 Просмотры
Последнее сообщение Anonymous
17 май 2024, 23:09
0 Ответы
28 Просмотры
Последнее сообщение Гость
24 сен 2023, 11:06
0 Ответы
31 Просмотры
Последнее сообщение Anonymous
07 дек 2023, 10:41