想把一个字符串输出到一个文件,FileOutputStream filewriter=new FileOutputStream("file.txt");但是这种方式是以字节数组的方式输出,不知道有没有字符串方式直接输出的方式?怎么输出?
我找了一个BufferedStreamWriter类,但是我不知道和上面的怎么转换,郁闷。高手救救我吧。

解决方案 »

  1.   

    下面有段copy的函数
    public int copy(InputStream input,OutputStream output) throws IOException {
         byte[] b = new byte[DEFAULT_BUFFER_SIZE];
         int value = 0,n = 0;
         while((n = input.read(b)) != -1) {
           output.write(b,0,n);
           value += n;
           }
           return value;
        }
        
        public int copy(OutputStream output,InputStream input) throws IOException {
         byte[] b = new byte[DEFAULT_BUFFER_SIZE];
         int value = 0,n=0;
         while((n = input.read(b)) != -1) {
              output.write(b,0,n);
              value += n;
             }
            return value;
        }    public void copy(byte[] b,OutputStream output) throws IOException {
          output.write(b);
        }    public void copy(byte[] b,Writer writer) throws IOException {
          ByteArrayInputStream in = new ByteArrayInputStream(b);
          copy(in, writer);
        }    public void copy(InputStream input,Writer writer) throws IOException {
          InputStreamReader in = new InputStreamReader(input);
          copy(in,writer);
        }    public void copy(Reader reader,OutputStream output) throws IOException {
          OutputStreamWriter writer = new OutputStreamWriter(output);
          copy(reader,writer);
        }    public void copy(String input,OutputStream output) throws IOException {
          StringReader in = new StringReader(input);
          OutputStreamWriter out = new OutputStreamWriter(output);
          copy(in,out);
          out.flush();
        }    public void copy(String input,Writer writer) throws IOException {
          writer.write(input);
        }    public int copy(Reader reader,Writer writer) throws IOException {
          char[] buffer = new char[DEFAULT_BUFFER_SIZE];
          int count = 0;
          int n = 0;
          while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
            count += n;
          }
          return count;
    }