자바 파일 입출력
- FileInputStream/FileOutputStream
12345678910111213141516171819202122232425262728public void f_InOutStream() throws IOException{FileInputStream in = null;FileOutputStream out = null;try{in = new FileInputStream("C:\\a.txt");out = new FileOutputStream("C:\\b.txt");int c;int n = 0;while((c = in.read()) != -1){out.write(c);n++;}System.out.println("InOutStream : " + n);}catch(Exception e){e.setStackTrace(null);}finally{if(in != null){in.close();}if(out != null){out.close();}}} - BufferedInputStream/BufferedOutputStream
12345678910111213141516171819202122232425262728public void f_BufferedInOutStream() throws IOException{BufferedInputStream in = null;BufferedOutputStream out = null;try{in = new BufferedInputStream(new FileInputStream("C:\\a.txt"));out = new BufferedOutputStream(new FileOutputStream("C:\\b.txt"));int c;int n = 0;while((c = in.read()) != -1){out.write(c);n++;}System.out.println("BufferedInOutStream : " + n);}catch(Exception e){e.setStackTrace(null);}finally{if(in != null){in.close();}if(out != null){out.close();}}} - FileReader/FileWriter
123456789101112131415161718192021222324252627public void f_ReadWriter() throws IOException{FileReader in = null;FileWriter out = null;try{in = new FileReader("C:\\a.txt");out = new FileWriter("C:\\b.txt");int c;int n = 0;while((c = in.read()) != -1){out.write(c);n++;}System.out.println("ReadWriter : " + n);}catch(Exception e){e.setStackTrace(null);}finally{if(in != null){in.close();}if(out != null){out.close();}}} - BufferedReader/BufferedWriter
12345678910111213141516171819202122232425262728public void f_BufferReadWriter() throws IOException{BufferedReader in = null;BufferedWriter out = null;try{in = new BufferedReader(new FileReader("C:\\a.txt"));out = new BufferedWriter(new FileWriter("C:\\b.txt"));int c;int n = 0;while((c = in.read()) != -1){out.write(c);n++;}System.out.println("BufferedReadWriter : " + n);}catch(Exception e){e.setStackTrace(null);}finally{if(in != null){in.close();}if(out != null){out.close();}}} - utf8과 euckr 선택하여 읽고 쓰기
12345678910111213141516171819202122232425262728public void f_BufferReadWriterUTF8() throws IOException{BufferedReader in = null;BufferedWriter out = null;try{in = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\a.txt"), "UTF-8")); //EUCKRout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\b.txt"),"UTF-8")); //EUCKRint c;int n = 0;while((c = in.read()) != -1){out.write(c);n++;}System.out.println("BufferedReadWriter : " + n);}catch(Exception e){e.setStackTrace(null);}finally{if(in != null){in.close();}if(out != null){out.close();}}} - Scanner/PrintWriter
1234567891011121314151617181920212223242526272829public void f_ScannerPrintWriter(){Scanner in = null;PrintWriter out = null;try{in = new Scanner(new BufferedReader(new FileReader("C:\\a.txt")));out = new PrintWriter(new BufferedWriter(new FileWriter("C:\\b.txt")));String c;int n = 0;while(in.hasNext()){c = in.nextLine();out.println(c);n++;}System.out.println("ScannerPrintWriter : " + n);}catch(Exception e){System.out.println("error");}finally{if(in != null){in.close();}if(out != null){out.close();}}} - DataInputStream/DataOutputStream
12345678910111213141516171819202122232425262728public void f_DataInOutStream() throws IOException{DataInputStream in = null;DataOutputStream out = null;try{out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\b.txt")));out.writeDouble(3.1415);out.writeInt(100);out.writeUTF("전주대학교");out.flush();in = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\b.txt")));System.out.println(in.readDouble());System.out.println(in.readInt());System.out.println(in.readUTF());}catch(Exception e){System.out.println("error");}finally{if(in != null){in.close();}if(out != null){out.close();}}} - ObjectInputStream/ObjectOutputStream
12345678910111213141516171819202122232425public void f_ObjectInOutStream() throws IOException{ObjectInputStream in = null;ObjectOutputStream out = null;try{out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\b.txt")));out.writeObject(new Date());out.flush();in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("C:\\b.txt")));Date d = (Date)in.readObject();System.out.println(d);}catch(Exception e){System.out.println("error");}finally{if(in != null){in.close();}if(out != null){out.close();}}} - File
1234567891011121314151617public void f_FileInfo(){//읽을 파일 이름 지정String fname = "C:\\a.txt";//File 객체 생성File f = new File(fname);System.out.println("파일 " + f.getName() + " 정보:");System.out.println();System.out.println("\t패스: " + f.getPath());System.out.println("\t절대패스: " + f.getAbsolutePath());System.out.println("\t부모: " + f.getParent());System.out.println("\t쓰기여부: " + f.canWrite());System.out.println("\t읽기여부: " + f.canRead());System.out.println("\t파일여부: " + f.isFile());System.out.println("\t폴더여부: " + f.isDirectory());System.out.println("\t수정일: " + new Date(f.lastModified()));System.out.println("\t파일크기: " + f.length());}