なぜかいきなりいままで動いていた
Javaのプログラムが動かなくなりました
開発環境はeclipsです
リストをつかっているプログラムが動きません
サイズがそんなに大きくないものを扱っても
うごきません
エラーとして
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
これがでてきます
すみません
ちょっと焦っていて
雑な説明しかできないのですが
よろしくお願いします
Java
1import java.io.BufferedReader; 2import java.io.File; 3import java.io.FileReader; 4import java.io.FileWriter; 5import java.io.IOException; 6import java.io.PrintWriter; 7 8import javax.swing.JFileChooser; 9import javax.swing.filechooser.FileFilter; 10 11public class TextDevider { 12 13 /** 14 * 1ファイルあたりの行数 15 */ 16 private final static int MAX_LINE = 500000; 17 18 /** 19 * 分割するファイル 20 */ 21 private File target = null; 22 23 public static void main(String[] args) { 24 JFileChooser chooser = new JFileChooser(); 25 chooser.setFileFilter(new FileFilter() { 26 public boolean accept(File file) { 27 return file.getName().endsWith(".txt"); 28 } 29 30 public String getDescription() { 31 return "テキストファイル"; 32 } 33 }); 34 35 if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) { 36 TextDevider devider = new TextDevider(chooser.getSelectedFile()); 37 38 try { 39 int num = devider.execDevide(); 40 System.out.println(num + "ファイルに分割"); 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 } else { 45 System.out.println("キャンセル"); 46 } 47 } 48 49 /** 50 * TextDeviderクラスのコンストラクタです。分割するテキストファイルを引数として渡します。 51 * 52 * @param file 分割するテキストファイル 53 */ 54 public TextDevider(final File file) { 55 this.target = file; 56 } 57 58 /** 59 * テキストファイルの分割を実行します。分割したファイルは分割するファイルと同じディレクトリに保存されます。 60 * 61 * @throws IOException 62 * @return 分割したファイル数 63 */ 64 public int execDevide() throws IOException { 65 BufferedReader reader = null; 66 PrintWriter writer = null; 67 boolean isFinished = false; 68 int fileCount = 0; 69 70 try { 71 reader = new BufferedReader(new FileReader(this.target)); 72 fileCount = 0; 73 74 while (!isFinished) { 75 File saveFile = new File(getSaveFilePath(fileCount)); 76 writer = new PrintWriter(new FileWriter(saveFile)); 77 String line = null; 78 fileCount++; 79 80 for (int lineCount = 0; lineCount < MAX_LINE 81 && (line = reader.readLine()) != null; lineCount++) { 82 writer.println(line); 83 } 84 85 isFinished = (line == null); 86 writer.close(); 87 writer = null; 88 } 89 } finally { 90 if (writer != null) { 91 writer.close(); 92 } 93 // writer.closeは例外を投げないので、reader.closeは確実に実行される 94 if (reader != null) { 95 reader.close(); 96 } 97 } 98 return fileCount; 99 } 100 101 /** 102 * 分割したファイルのパスを決定します。 103 * 104 * @param count 105 * 分割したファイルの順番を表す整数値 106 * @return 分割したファイルのパス 107 */ 108 private String getSaveFilePath(final int count) { 109 return this.target.getPath() + "_" + count; 110 } 111} 112 113
回答2件
あなたの回答
tips
プレビュー