文件流示例

来源:互联网 发布:大数据分析道与术 编辑:程序博客网 时间:2024/06/06 07:07
 

package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

public class test {

 /**
  * 读取文件 ,获取文件内容, 假设目前f盘符下的一个a.txt文件有内容0123456789
  *
  * @param filePath
  * @return 根据文件路径,获取的文件内容。
  */
 public String getFileContent(String filePath) {
  String fileContent = "";
  try {
   BufferedReader br = new BufferedReader(new FileReader(filePath));
   try {
    fileContent = br.readLine();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return fileContent;
 }

 /**
  * 截取文件内容方法 ,假设每隔3位截取
  *
  * @param dynamicValue
  * @return 经过处理之后的文件内容。
  */
 public String getDynamicValue(String path) {
  String getDynamicValue = "";
  File f = new File(path);
  File[] fArry = f.listFiles();
  String reader1 = "";
  for (int i = 0; i < fArry.length; i++) {
   if (fArry[i].isFile()) {
    String path1 = fArry[i].getAbsolutePath();
    try {
     BufferedReader reader = new BufferedReader(new FileReader(path1));
     reader1 = reader.readLine();
     int lineCount = 0;
     String value = "";
     int namei = 0;
     while(reader1 != null){
      lineCount++;
      if(lineCount%3 == 0){
       namei++;
       value=value+reader1+";";
       System.out.print(value);
       this.createFileBy(value, "f:/test/", "test"+(namei), ".txt");
      }else{
       value=reader1+"\r\n";
      }
      reader1 = reader.readLine();
     }
//     System.out.println("lineCount:"+lineCount);
     reader.close();
    } catch (Exception e) {
     e.printStackTrace();
    }

   }
  }
  return getDynamicValue;
 }

 /**
  * 用处理好的数据生成文件
  *
  * @param filePath
  */
 public void createFileBy(String getValue, String pathFile, String fileName,
   String fileType) {
  //this.createFileBy(reader1, "f:/test", "test"+i, ".txt");

//  String[] value = getValue.split(";");
//  String createFileName = "";
//  String newStrPath = "";
//  for (int i = 0; i < value.length; i++) {
//   createFileName = fileName + i;// 文件名
//   newStrPath = pathFile + createFileName + fileType;
//      newStrPath = pathFile + fileName + fileType;
      pathFile = pathFile + fileName + fileType;
   File file = new File(pathFile);// 根据所在路径创建文件
   try {
    OutputStream os = new FileOutputStream(file);
//    System.out.println("===================os:"+getValue);
    try {
     os.write(getValue.getBytes());// 往文件里面输入内容。
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
//  }
 }

 /**
  * @param args
  */
 public static void main(String[] args) {
  test t = new test();
  String ret = t.getDynamicValue("f:/Test/");
//  t.getNum("f:/test/");
  
//  String getDynamicValue = t.getDynamicValue("f:/test/");
//  System.out.println(getDynamicValue);
  

 }
 
 public String getNum(String path) {
  File f = new File(path);
  File[] fArry = f.listFiles();
  String reader1 = "";
  for (int i = 0; i < fArry.length; i++) {
   if (fArry[i].isFile()) {
    String line = null;
    String path1 = fArry[i].getAbsolutePath();
    try {
     BufferedReader reader = new BufferedReader(new FileReader(path1));
     reader1 = reader.readLine();
     String lines = "";
     int lineCount = 0;
     while(reader1 != null){
      lineCount++;
      if(lineCount%3 == 0){
       System.out.println(";"); 
      }
      System.out.print(reader1);
      reader1 = reader.readLine();
     }
     System.out.println();
//     System.out.println("行数:" + lineCount);
     reader.close();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  }
  return reader1;
 }

}