diff --git a/src/main/java/cit/PureATN/FileReadWriter.java b/src/main/java/cit/PureATN/FileReadWriter.java new file mode 100644 index 0000000..6dd468d --- /dev/null +++ b/src/main/java/cit/PureATN/FileReadWriter.java @@ -0,0 +1,192 @@ +package cit.PureATN; +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.util.ArrayList; + +public class FileReadWriter { + + public static ArrayList getLinesListFromResource(String path){ + ClassLoader cl = FileReadWriter.class.getClassLoader(); + + if (cl == null) { + System.err.println("Resource not found: " + path); + return null; + } else { + InputStream ris = cl.getResourceAsStream(path); + return getLinesListFromInputStream(ris, path); + } + } + public static ArrayList getLinesListFromInputStream(InputStream is, String name){ + ArrayList list = new ArrayList(); + try { + BufferedReader br = new BufferedReader(new InputStreamReader(is)); + String line = null; + /* ファイル読み込み */ + while ((line = br.readLine()) != null) { + list.add(line); + } + /* ファイルを閉じます */ + br.close(); + } catch (IOException err) { + System.out.println("ReadError: "+name); + } + return list; + } + + public static ArrayList getLinesList(String fn, boolean createIfNotExist){ + File f = new File(fn); + if (createIfNotExist){ + if (!f.exists()){ + try { + if (f.createNewFile()){ + System.out.println("New file "+fn+" Created."); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + return getLinesList(fn); + } + + public static ArrayList getLinesList(String fn){ + try { + return getLinesListFromInputStream(new FileInputStream(fn), fn); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return null; + } + /* ファイルを読み込む */ + public static String[] getLines(String fn) { + + ArrayList list = getLinesList(fn); + return (String[]) list.toArray(new String[list.size()]); + } + + public static String[] getLinesFromResource(String path){ + ArrayList list = getLinesListFromResource(path); + return (String[]) list.toArray(new String[list.size()]); + } + + /** 配列をファイルに書き込む + * + * @param fn ファイル名 + * @param data 文字列の配列(各行が要素) + */ + public static void putLines(String fn, String data[]) { + try { + FileWriter filewriter = new FileWriter(fn, false); + for (int i = 0; i < data.length; i++) { + /* 改行文字追加して */ + /* 文字列を書き込みます */ + filewriter.write(data[i]+System.getProperty("line.separator")); + } + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* ファイルに書き込む */ + public static void putLines(String fn, ArrayList data) { +// System.out.println(fn); + try { + FileWriter filewriter = new FileWriter(fn, false); + for (String d : data) { + /* 改行文字追加 */ + d = d + "\r\n"; + /* 文字列を書き込みます */ + filewriter.write(d); + } + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* ファイルに書き込む */ + public static void putStringToFile(String fn, String out) { +// System.out.println(fn); + try { + FileWriter filewriter = new FileWriter(fn, false); + filewriter.write(out); + /* ファイルを閉じます */ + filewriter.close(); + } catch (IOException e) { + System.out.println("WriteError:" + fn); + } + } + + /* 文字列を分割して配列で返します */ + public static String[] split(String delim, String text) { + int index = -1; + ArrayList list = new ArrayList(); + while ((index = text.indexOf(delim)) != -1) { + list.add(text.substring(0, index)); + text = text.substring(index + delim.length()); + } + list.add(text); + String[] ret = (String[]) list.toArray(new String[list.size()]); + return ret; + } + + public static void writeBytesToFile(String filepath, byte[] ba){ + OutputStream fos = null; + DataOutputStream dos = null; + try{ + fos = new FileOutputStream(filepath); + dos = new DataOutputStream(fos); + dos.write(ba); + dos.close(); + fos.close(); + }catch(IOException iex){ + iex.printStackTrace(System.out); + } + } + + public static byte[] readBytesFromFile(String path){ + File f = new File(path); + if (!f.exists()) System.out.println("No file "+path); + + BufferedInputStream in; + ByteArrayOutputStream varyBuf = new ByteArrayOutputStream(); + final int LS = 1024; + int b; + try { + in = new BufferedInputStream(new FileInputStream(path)); + byte buf[] = new byte[LS]; + while((b = in.read(buf, 0, buf.length)) != -1 ) { + varyBuf.write(buf,0,b) ; + } + varyBuf.close(); + in.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return varyBuf.toByteArray(); + } + public static ArrayList buffer; + public static void bufclear() { + if (buffer == null) buffer = new ArrayList(); + else buffer.clear(); + } + public static void bufSave(String fn){ + FileReadWriter.putLines(fn, buffer); + } +}