读写文件的操作

一.以字节的方式读取和写入

 

InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }

二.文件的读取

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.test.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 *
 * @author ghh
 */
public class ScanFile {

    public void scanFiles(File file) {
        try {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    scanFiles(new File(files[i].getAbsolutePath()));
                    String path = files[i].getAbsolutePath();
                    int index = path.indexOf("mirror");
                    path = path.substring(index + 7).trim();
                    System.out.println("path  " + path);
                    File f = new File("/home/zdl/fw.txt");
                    this.readFile(f, path);

                }
            }
        } catch (Exception e) {
        }
    }

    public void readFile(File fw, String path) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fw)));
            String data = null;
            String str = null;
            while ((data = br.readLine()) != null) {
                str = "http://" + path + "/" + data.trim();
                this.write(str);
                System.out.println(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write(String path) {
        try {
            File ganji = new File("/home/zdl/ganji.lst");
            if (!ganji.isFile()) {
                ganji.createNewFile();
            }
            FileWriter fw = new FileWriter(ganji, true);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(path);
            pw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write1(File file) {
        try {
            File[] files = file.listFiles();
            File ganji = new File("/home/zdl/ganji.lst");
            FileWriter fw = new FileWriter(ganji, true);
            PrintWriter pw = new PrintWriter(fw);
            if (!ganji.isFile()) {
                ganji.createNewFile();
            }
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    write1(new File(files[i].getAbsolutePath()));
                } else {
                    String path = files[i].getAbsolutePath();
                    int index = path.indexOf("mirror");
                    path ="http://" + path.substring(index + 7, path.length() - 11).trim();
                    
                    System.out.println("path  " + path);
                    pw.println(path);
                }
            }
            pw.flush();
            pw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getCity(File file) {
        File[] files = file.listFiles();
        System.out.println("size  "+files.length);
        for(int i = 0; i<files.length; i++) {
            if(files[i].isDirectory()) {
                System.out.println(files[i].getName());
            }
        }
    }
    
    public static void main(String[] args) {
        ScanFile s = new ScanFile();
        File file = new File("/home/zdl/Downloads/mirror");
//        File f = new File("/home/zdl/fw.txt");
//        s.scanFile(file);
//        s.readFile(f);
//        s.write1(file);
        s.getCity(file);
    }
}

三.读文件
bufferedReader br = new bufferedReader(new FileReader(file))

四.写入文件
printwriter pw = new PrintWriter(new FileoutputStream(file))


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值