Чтение большого файла с кодировкой [дубликат]JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Чтение большого файла с кодировкой [дубликат]

Сообщение Anonymous »

Мой файл весит 14 ГБ, и я хотел бы прочитать его построчно и экспортировать в файл Excel.
Поскольку файл содержит разные языки, например китайский и английский, я попытался использовать FileInputStream с UTF-16 для чтения данных, но в результате получилось:

java.lang.OutOfMemoryError: пространство кучи Java

Я пытался увеличить пространство кучи, но проблема не устранена. Как мне изменить код чтения файлов?
createExcel(); //open a excel file
try {

//success but cannot read and output for different language
//br = new BufferedReader(
// new FileReader("C:\\Users\\brian_000\\Desktop\\appdatafile.json"));

//result in java.lang.OutOfMemoryError: Java heap space
br = new BufferedReader(new InputStreamReader(
new FileInputStream("C:\\Users\\brian_000\\Desktop\\appdatafile.json"),
"UTF-16"));

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println("cann be print");

String line;
int i=0;
try {
while ((line = br.readLine()) != null) {
// process the line.
try{
System.out.println("cannot be print");
//some statement for storing the data in variables.

//a function for writing the variable into excel
writeToExcel(platform,kind,title,shareUrl,contentRating,userRatingCount,averageUserRating
,marketLanguage,pricing
,majorVersionNumber,releaseDate,downloadsCount);

}
catch(com.google.gson.JsonSyntaxException exception){
System.out.println("error");
}

// trying to get the first 1000rows
i++;

if(i==1000){
br.close();

break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

closeExcel();

public static void writeToExcel(String platform,String kind,String title,String shareUrl,String contentRating,String userRatingCount,String averageUserRating
,String marketLanguage,String pricing,String majorVersionNumber,String releaseDate,String downloadsCount){

currentRow++;
System.out.println(currentRow);

if(currentRow>1000000){
currentsheet++;
sheet = workbook.createSheet("apps"+currentsheet, 0);
createFristRow();
currentRow=1;
}

try {

//character id
Label label = new Label(0, currentRow, String.valueOf(currentRow), cellFormat);
sheet.addCell(label);

//12 of statements for write the data to excel
label = new Label(1, currentRow, platform, cellFormat);
sheet.addCell(label);

} catch (WriteException e) {
e.printStackTrace();
}

Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»