

1 import java.io.*; 2 3 /** 4 * @Author: Lambert 5 * @Date: 2018-12-31 22:45 6 * @Description: 7 * java 写入文件 8 */ 9 public class fileStreamTest2 { 10 public static void main(String[] args) throws IOException { 11 File file =new File("a.txt"); 12 /** 13 * 构建FileOutputStream对象,文件不存在会自动新建 14 */ 15 FileOutputStream fop=new FileOutputStream(file); 16 /** 17 * 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk 18 */ 19 OutputStreamWriter writer =new OutputStreamWriter(fop); 20 /** 21 * 写入文件 22 */ 23 writer.append("abc"); 24 /** 25 * 关关闭写入流,同时会把缓冲区内容写入文件 26 */ 27 writer.close(); 28 /** 29 * 关闭输出流,释放系统资源 30 */ 31 fop.close(); 32 33 34 } 35 }


1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 6 /** 7 * @Author: Lambert 8 * @Date: 2018-12-31 22:59 9 * @Description: 10 */ 11 public class fileStreamTest3 { 12 public static void main(String[] args) throws IOException { 13 /** 14 * 定义文本 15 */ 16 File file = new File("a.txt"); 17 /** 18 * 构建FileInputStream对象(将文本转化为流) 19 */ 20 FileInputStream fip = new FileInputStream(file); 21 /** 22 * 构建InputStreamReader对象,编码定义UTF-8 23 */ 24 InputStreamReader reader = new InputStreamReader(fip, "UTF-8"); 25 StringBuffer stringBuffer = new StringBuffer(); 26 while (reader.ready()) { 27 /** 28 * 转成char加到StringBuffer对象中 29 */ 30 stringBuffer.append((char) reader.read()); 31 } 32 /** 33 * 打印读出来对象 34 */ 35 System.out.println(stringBuffer.toString()); 36 /** 37 * 关闭读取流 38 */ 39 reader.close(); 40 /** 41 * 关闭输入流,释放系统资源 42 */ 43 fip.close(); 44 } 45 }


1 import java.io.*; 2 import java.util.ArrayList; 3 import java.util.Collections; 4 import java.util.List; 5 6 /** 7 * @program: JavaUtilTools 8 * @description: 9 * @author: Mr.LI 10 * @create: 2018-09-09 23:07 11 **/ 12 13 public class TXTAnalysis { 14 public static void main(String[] args) throws IOException { 15 try { 16 //定义文件路径 17 File file = new File("/workspace/ptest/param/userinfo_160w_5wPerTable.txt"); 18 // File file = new File("/Users/lizhen/Downloads/userInfoForRealTimeTest.txt"); 19 //创建了一个字符写入流的缓冲区对象,并和指定要被缓冲的流对象相关联 20 BufferedReader bf = new BufferedReader(new FileReader(file)); 21 String str; 22 //遍历数组 23 List<String> lists = new ArrayList<>(); 24 //将文件循环读取每一行 25 while ((str = bf.readLine()) != null) { 26 lists.add(str); 27 } 28 // 将内容随机打乱 29 Collections.shuffle(lists); 30 //循环遍历打印结果 31 File writename = new File("/workspace/ptest/param/userinfo_160w_5wPerTable_01.txt"); 32 // 相对路径,如果没有则要建立一个新的output。txt文 33 writename.createNewFile(); 34 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); 35 for (int i = 0; i < lists.size(); i++) { 36 //创建写入文件 37 out.write(lists.get(i) + "\n"); 38 } 39 out.flush(); 40 out.close(); 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 } 45 }
正则提取文件
方法一:自己写工具类实现脚本
1、工具类部分:


1 package com.example.demo.util; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStreamReader; 7 8 /** 9 * @Author: Lambert 10 * @Date: 2019-01-06 20:29 11 * @Description: 12 */ 13 public class FileProcessing { 14 /** 15 * 读取文件打印 16 * @return 17 * @throws IOException 18 */ 19 public String fileRead() throws IOException { 20 //定义本路径 21 File f = new File("/Users/lizhen/PycharmProjects/untitled/test/test.txt"); 22 //文件输入流对象定义 23 FileInputStream fip = new FileInputStream(f); 24 //读取流 25 InputStreamReader reader = new InputStreamReader(fip, "UTF-8"); 26 // 27 StringBuffer stringBuffer = new StringBuffer(); 28 while (reader.ready()) { 29 /** 30 * 转成char加到StringBuffer对象中 31 */ 32 stringBuffer.append((char) reader.read()); 33 } 34 /** 35 * 打印读出来对象 36 */ 37 // System.out.println(stringBuffer.toString()); 38 /** 39 * 关闭读取流 40 */ 41 reader.close(); 42 /** 43 * 关闭输入流,释放系统资源 44 */ 45 fip.close(); 46 return stringBuffer.toString(); 47 } 48 }
2、代码测试打印部分


1 package com.example.demo.test; 2 3 import com.example.demo.util.FileProcessing; 4 5 import java.io.*; 6 import java.util.regex.Matcher; 7 import java.util.regex.Pattern; 8 9 /** 10 * @Author: Lambert 11 * @Date: 2019-01-06 19:38 12 * @Description: 13 */ 14 public class Test { 15 public static void main(String[] args) throws IOException { 16 //调用工具类方法 17 FileProcessing fileProcessing =new FileProcessing(); 18 //定义正则规则 19 Pattern compile =Pattern.compile("<action>(.*)</action>"); 20 //方法调用执行 21 Matcher matcher =compile.matcher(fileProcessing.fileRead()); 22 //循环遍历方法 23 while (matcher.find()){ 24 //执行正则执行后规则打印 25 System.out.println(matcher.group(1)); 26 } 27 } 28 }
方法二:
引入jar包:commons-io


<!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency>
读入字节数实现
import java.io.IOException;
/**
* @Author: Lambert
* @Date: 2019-02-24 20:24
* @Description:
*/
public class Test {
public static void main(String[] args) {
System.out.println("Hello World!");
//读入1024 字节的buffer
byte[] buffer = new byte[1024];
try {
// 读入数据长度为len
int len = System.in.read(buffer);
String s = new String(buffer, 0, len);
System.out.println("读到了" + len + "字节");
System.out.println(s);
System.out.println("s的长度是:" + s.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}