IO流整理

 

package com.test.reader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class ReaderDemo
{
    public static void main(String[] args)
    {
        Reader r = null;
//        try
//        {
//            r = new FileReader(Thread.currentThread().getContextClassLoader().getResource("com/test/reader/1.txt").getFile());
//        }
//        catch (FileNotFoundException e)
//        {
//            e.printStackTrace();
//        }

//文件中的内容跟字符串s中的内容是一样的
        String s = "wepftwgpempgdsg/ngmepgm;sgf/ns,gf'dsmgfd;smgfd/n,;sdfm ng;df/nmg;sdgf;/n";
        r = new StringReader(s);
        BufferedReader reader = new BufferedReader(r);
        StringBuffer sb = new StringBuffer();
        String line;
        try
        {
            line = reader.readLine();
            while(null != line)
            {
                sb.append(line);
                line = reader.readLine();
                sb.append("/n");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != r)
            {
                try
                {
                    r.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }

            if (null != reader)
            {
                try
                {
                    reader.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        System.out.println(sb.toString());
    }
}

 

 

 

 

package com.test.writer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo
{
    public static void main(String[] args)
    {
//        Writer w = new StringWriter();
//        try
//        {
//            w.write("wefowge", 2, 5);
//            System.out.println(w.toString());
//        }
//        catch (IOException e)
//        {
//            e.printStackTrace();
//        }


        //文件读取写入
        Reader r = null;
        try
        {
            r = new FileReader(Thread.currentThread().getContextClassLoader().getResource("com/test/reader/1.txt").getFile());
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
//        BufferedReader reader = new BufferedReader(r);

        Writer fw = null;
        try
        {
            fw = new FileWriter("d:/2.txt");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        BufferedWriter bw = new BufferedWriter(fw);
        try
        {
//            String line = reader.readLine();
//            while (null != line)
//            {
//                bw.write(line);
//                bw.newLine();
//                line = reader.readLine();
//
//            }
            char[] ch = new char[13];
            int n = 0;
            while ((n = r.read(ch, 0, 13)) != -1)
            {
                bw.flush();
                bw.write(ch, 0, n);
                if (n == 13)
                {
                    ch = new char[13];
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != r)
            {
                try
                {
                    r.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (bw != null)
            {
                try
                {
                    bw.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

 

 

package com.test.stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class StreamDemo
{


    public static void main(String[] args)
    {
//      InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/test/stream/1.txt");
        File inputFile = new File("d:/1.txt");
        File outputFile = new File("d:/2.txt");
        InputStream is = null;
        OutputStream os = null;
        try
        {
            is = new FileInputStream(inputFile);

            //字节输入流转为字符输入流
            InputStreamReader reader = new InputStreamReader(is);

 

            os = new FileOutputStream(outputFile);

           // 字节输出流转换为字符输出流

            OutputStreamWriter writer = new OutputStreamWriter(os);
            int n = 0;
            while ((n = is.read()) != -1)
            {
                os.write(n);
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != is)
            {
                try
                {
                    is.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            if (null != os)
            {
                try
                {
                    os.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

下面一段是使用Servlet向页面使用流的方式输出内容

/**
  * 只输出不跳转,文件中的内容只会输出到浏览器上,并不能跳转
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//     doPost(request, response);
     ServletOutputStream os = response.getOutputStream();
     InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/test/1.txt");
     byte[] b = new byte[10];
     int n = 0;
     while ((n = is.read(b, 0, 10)) != -1)
     {
         os.write(b, 0, n);
         if (n == 10)
         {
             b = new byte[10];
         }
     }
     os.close();
     is.close();
 }

 

//这种方式在页面上可以使用小脚本对流中的内容进行读取

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     Writer writer = response.getWriter();
        writer.write("abcd");
        writer.close();
        if (!response.isCommitted())
        {
            request.getRequestDispatcher("result.jsp").forward(request, response);
        }
        return;
 }

 

调用post方式时,可以使用小脚本在页面上获取流信息

<%
  StringBuffer sb = new StringBuffer();
  BufferedReader reader = request.getReader();
  String str = reader.readLine();
  while (null != str)
  {
   sb.append(str);
   str = reader.readLine();
  }
  reader.close();
 %>
 

 ----------------------------------------------------------
 <%=sb.toString() %>

你可能感兴趣的:(Java基础,io,null,string,file,byte,class)