文件流示例
来源:互联网 发布:大数据分析道与术 编辑:程序博客网 时间: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;
}
}
- 文件流示例
- 文件流示例
- FileInputStream(文件输入流) 示例
- FileOutputStream(文件输出流) 示例
- 写视频流文件测试源码示例
- FileInputStream(文件输入、输出流) 示例
- JAVA NIO文件映射、通道、流读写文件示例
- Socket传输文件示例
- 示例:文件上传
- WinInet 下载文件示例
- WinInet 下载文件示例
- vimrc文件示例
- PE文件病毒感染示例
- JAVA文件操作示例
- 文件的上传下载示例
- JS文件操作示例
- JAVA文件操作示例
- Struts2上传文件示例
- 用 MDX 创建时间命名集总结 SSAS
- NIO应用
- 有关使用 GPS Intermediate Driver 无法获取到GPS数据的一个问题
- cassandra学习笔记一
- db2命令行远程连接数据库
- 文件流示例
- chroot环境安装Qt 4.7.2
- 多线程实现Galgame的字幕显示
- FLex资源
- 2012年项目进展情况和未来计划
- Concurrent应用
- 基于用户投票的排名算法(一):Delicious和Hacker News
- lvs+keepalived和haproxy+heartbeat区别
- 产生JS指定范围随机数(包含上限数和下限数)