본문 바로가기

Java

[Java] FileInputStream, FileOutPutStream...

출처 주넝아???? | 네이아
원본 http://blog.naver.com/akswnsjd1/60035968311

========
자바
========
!!FileInputStream,FileOutputStream,InputStreamReader,bufferedWriter,bufferedReader,URL
======================================
복사

import java.io.*;

class  FileCopyTest
{
 public static void main(String[] args)
 {
  try{
  FileInputStream fis = new FileInputStream ("sing.jpg") ;
  FileOutputStream fos = new FileOutputStream ("sing1.jpg");
  int n = 0;
  while ((n=fis.read())!=-1)
  {
   fos.write(n);
  }
  fos.close();
  fis.close();
 
  }catch(Exception e){}
 }
}
==================================================
다른 복사

import java.io.*;

class  FileCopyTest1
{
 public static void main(String[] args)
 {
  try{
  byte[] bt = new byte[1024];

  FileInputStream fis = new FileInputStream ("sing.jpg") ;
  FileOutputStream fos = new FileOutputStream ("sing21.jpg");
 
 
  int i;
  while((i=fis.read(bt))!=-1) fos.write (bt,0,i);
  fos.close();
  fis.close();
 
  }catch(Exception e){}
 }
}
==================================================
txt파일을 화면에 출력

import java.io.*;
class  InputStreamReaderTest
{
 public static void main(String[] args)
 {
  try{
  InputStreamReader isr = new InputStreamReader(new FileInputStream("InputStreamReaderTest.java"));
  int i=0;
  while((i=isr.read ())!=-1){
   System.out.print((char)i);
  }

  }catch(Exception e){}

 }
}
============================================
BufferedReader를 이용하고 bufferedWriter를사용하여 파일복사

import java.io.*;
class  BufferedReaderTest
{
 public static void main(String[] args)
 {
  try{
   int i;
   BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream("InputStreamReaderTest.java")));
   BufferedWriter bw = new BufferedWriter (new OutputStreamWriter(new FileOutputStream("a1.java")));
   
   String line = null;

   while((line=br.readLine())!=null){
    bw.write (line,0,line.length());
    bw.newLine();
   }
   bw.close();
   br.close();

  }catch(Exception e){}

 }
}
=====================================================
url객채 생성
inputstream 을 얻는다
inputstreamreader 얻는다
bufferedreader 생성
readline으로 모니터 출력

url로
url커낵션
===================================================
url 에 해당하는 html 카피

import java.net.*;
import java.io.*;

class  URLTest
{
 public static void main(String[] args)
 {
  try{
   int i;
  URL url = new URL("http://www.yahoo.co.kr/");
  URLConnection uc = url.openConnection ();
  InputStream is = uc.getInputStream ();
 
  BufferedReader br = new BufferedReader( new InputStreamReader(is));
  BufferedWriter bw = new BufferedWriter (new OutputStreamWriter(new FileOutputStream("a.html")));
  String line = null;
  while((line=br.readLine())!=null){
   //System.out.println(line);
   bw.write (line,0,line.length());
   bw.newLine();
  }
  bw.close();

  }catch(Exception e){}

 }
}
===================================================
숙제
url 이미지 가지고와서 저장하기

==============================================

import java.io.*;
import java.net.*;

class GetURLImage
{
 public static void main(String[] args)
 {
  try{
   URL url = new URL("http://wstatic.naver.com/w/n_c600.gif");
   URLConnection uc = url.openConnection ();
   InputStream is = uc.getInputStream ();
   
   BufferedInputStream bs = new BufferedInputStream(is);
   
   FileOutputStream fos = new FileOutputStream ("a.gif");
   
   int n = 0;
   while ((n=bs.read())!=-1)
   {
    fos.write(n);
   }
   fos.close();


  }catch(Exception e){}
 }
}

=============================================================