最近刷题做了一些对于txt文件的读写操作,发现设计IO数据流的操作方法较多,比较杂乱,而且方法较容易忘掉,所以把代码记录下来方便记忆。
而最近也在研究BIO和NIO对于socket的数据流的处理也有一些总结,将在以后的论文中进行补充。
对于TXT文件的读操作:
例子一://说明 完成从input.txt,文件读取数据,按正则表达式分类,然后统计单词出现次数(升序),存到out.txt中(程序写的很渣)
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.*;
public final class Test {
/*****************************************************************************
Description : 实现整数排序,即先将从A输入的整型数序列进行排序,剔除重复整型数,输出得到的升序序列B;
Input : array_A 输入参数,输入待排序整型数序列A
Return : 排序后的整型数序列
*****************************************************************************/
public static void main(String[] args) {
SortType type = SortType.WORDASC;
sortWords("input.txt","e:out.txt",type);
}
public enum SortType {
WORDASC, // 升序
WORDDSC, // 降序
WORDCOUNT; // 按出现次数排序
}
public static boolean sortWords(String srcPathName,
String dstPathName, SortType type) {
File file = new File(srcPathName);
BufferedReader bf=null;
InputStreamReader read=null;
try{
read = new InputStreamReader(new FileInputStream(file));
bf = new BufferedReader(read);
String readtext="";
String s ;
while((s=bf.readLine())!=null){
readtext+=(s+" ");
}
read.close();
String[] ss = readtext.split("[ |\\.|\\,|/.|/:|/;|/?|/!|\\”]+");
Map<String,Integer> map = new HashMap<String,Integer>();
int count=0;
for(int i=0;i<ss.length;i++){
if(map.containsKey(ss[i])){
count =map.get(ss[i])+1;
map.put(ss[i], count);
}else{
map.put(ss[i], 1);
}
}
int[][] sort_int = new int[map.size()][2];
String[] sort_str = new String[map.size()];
int t=0;
int q=0;
for(Map.Entry<String,Integer> entry:map.entrySet()){
sort_int[t++][0]=entry.getValue();
sort_int[q][1]=q;
sort_str[q++]=entry.getKey();
}
for(int i=0;i<map.size()-1;i++){
for(int j=i+1;j<map.size();j++){
if(sort_int[i][0]>sort_int[j][0]){
int temp;temp=sort_int[i][0];sort_int[i][0]=sort_int[j][0];sort_int[j][0]=temp;
temp=sort_int[i][1];sort_int[i][1]=sort_int[j][1];sort_int[j][1]=temp;
}
}
}
File file1 = new File(dstPathName);
FileOutputStream out=new FileOutputStream(file1); //如果追加方式用true
String sb;
for(int i=0;i<map.size();i++){
System.out.println("++++++++++++++++");
sb=sort_str[sort_int[i][1]]+":"+sort_int[i][0]+"\r\n";
out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
System.out.println("---------------");
}
read.close();
// br.close();
out.close();
/*
int[] count = new int[ss.length];
for(int i=0;i<ss.length;i++){
count[i]=1;
}
for(int i=0;i<ss.length-1;i++){
if(count[i]==-1)
continue;
for(int j=i+1;j<ss.length;j++){
if(ss[i].equals(ss[j])){
count[i]++;
count[j]=-1;
}
}
}
* */
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return false;
}
}
例子二:读
1.从文件中安单个字符读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("text.txt");
int ch=input.read();//读入一个字节 while(ch!=-1)
while(ch!=-1){
System.out.print((char)ch); ch=input.read(); }
input.close();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}import java.io.BufferedReader;
2.从文件中安byte数组读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("input.txt");
byte[] ch=new byte[1024];//读入一个字节 while(ch!=-1)
int len;String s="";
while((len=input.read(ch))!=-1){
s+=new String(ch); }
System.out.println(s);
input.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}
3.从文件中安char数组读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(input);
char[] ch=new char[1024];//读入一个字节 while(ch!=-1)
int len;String s="";
while((len=isr.read(ch))!=-1){
s+=new String(ch); }
System.out.println(s);
input.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}
例子三:写
1.BufferedWriter写入文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.FileOutputStream写入文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
而最近也在研究BIO和NIO对于socket的数据流的处理也有一些总结,将在以后的论文中进行补充。
对于TXT文件的读操作:
例子一://说明 完成从input.txt,文件读取数据,按正则表达式分类,然后统计单词出现次数(升序),存到out.txt中(程序写的很渣)
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.*;
import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
import java.io.*;
public final class Test {
/*****************************************************************************
Description : 实现整数排序,即先将从A输入的整型数序列进行排序,剔除重复整型数,输出得到的升序序列B;
Input : array_A 输入参数,输入待排序整型数序列A
Return : 排序后的整型数序列
*****************************************************************************/
public static void main(String[] args) {
SortType type = SortType.WORDASC;
sortWords("input.txt","e:out.txt",type);
}
public enum SortType {
WORDASC, // 升序
WORDDSC, // 降序
WORDCOUNT; // 按出现次数排序
}
public static boolean sortWords(String srcPathName,
String dstPathName, SortType type) {
File file = new File(srcPathName);
BufferedReader bf=null;
InputStreamReader read=null;
try{
read = new InputStreamReader(new FileInputStream(file));
bf = new BufferedReader(read);
String readtext="";
String s ;
while((s=bf.readLine())!=null){
readtext+=(s+" ");
}
read.close();
String[] ss = readtext.split("[ |\\.|\\,|/.|/:|/;|/?|/!|\\”]+");
Map<String,Integer> map = new HashMap<String,Integer>();
int count=0;
for(int i=0;i<ss.length;i++){
if(map.containsKey(ss[i])){
count =map.get(ss[i])+1;
map.put(ss[i], count);
}else{
map.put(ss[i], 1);
}
}
int[][] sort_int = new int[map.size()][2];
String[] sort_str = new String[map.size()];
int t=0;
int q=0;
for(Map.Entry<String,Integer> entry:map.entrySet()){
sort_int[t++][0]=entry.getValue();
sort_int[q][1]=q;
sort_str[q++]=entry.getKey();
}
for(int i=0;i<map.size()-1;i++){
for(int j=i+1;j<map.size();j++){
if(sort_int[i][0]>sort_int[j][0]){
int temp;temp=sort_int[i][0];sort_int[i][0]=sort_int[j][0];sort_int[j][0]=temp;
temp=sort_int[i][1];sort_int[i][1]=sort_int[j][1];sort_int[j][1]=temp;
}
}
}
File file1 = new File(dstPathName);
FileOutputStream out=new FileOutputStream(file1); //如果追加方式用true
String sb;
for(int i=0;i<map.size();i++){
System.out.println("++++++++++++++++");
sb=sort_str[sort_int[i][1]]+":"+sort_int[i][0]+"\r\n";
out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集
System.out.println("---------------");
}
read.close();
// br.close();
out.close();
/*
int[] count = new int[ss.length];
for(int i=0;i<ss.length;i++){
count[i]=1;
}
for(int i=0;i<ss.length-1;i++){
if(count[i]==-1)
continue;
for(int j=i+1;j<ss.length;j++){
if(ss[i].equals(ss[j])){
count[i]++;
count[j]=-1;
}
}
}
* */
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
return false;
}
}
例子二:读
1.从文件中安单个字符读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("text.txt");
int ch=input.read();//读入一个字节 while(ch!=-1)
while(ch!=-1){
System.out.print((char)ch); ch=input.read(); }
input.close();
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}import java.io.BufferedReader;
2.从文件中安byte数组读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("input.txt");
byte[] ch=new byte[1024];//读入一个字节 while(ch!=-1)
int len;String s="";
while((len=input.read(ch))!=-1){
s+=new String(ch); }
System.out.println(s);
input.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}
3.从文件中安char数组读取数据
public final class Demo {
public static void main(String[] args) {
try{
FileInputStream input=new FileInputStream("input.txt");
InputStreamReader isr = new InputStreamReader(input);
char[] ch=new char[1024];//读入一个字节 while(ch!=-1)
int len;String s="";
while((len=isr.read(ch))!=-1){
s+=new String(ch); }
System.out.println(s);
input.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
}
}
}
例子三:写
1.BufferedWriter写入文件
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class WriteToFileExample {
public static void main(String[] args) {
try {
String content = "This is the content to write into file";
File file = new File("/users/mkyong/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.FileOutputStream写入文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
FileOutputStream fop = null;
File file;
String content = "This is the text content";
try {
file = new File("c:/newfile.txt");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}