「java记录文件」java统计文件大小

博主:adminadmin 2023-03-20 07:52:06 487

本篇文章给大家谈谈java记录文件,以及java统计文件大小对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java修改文件中的记录

建议:可以用xml来描述记录结构,解析起来也方便快捷

以下提供对象模型以及实现.如果认可代码,请加分 200,谢谢!

先看看记录的每一行是如何用JAVA面向对象思想描述

public interface RowData {

public int getColumns();

public Object getDataAtColumn(int column);

}

再看解析器有哪些功能可对外提供:

import java.io.*;

public interface RowDataParser {

public RowData[] parser(InputStream in) throws IOException;

public void save(RowData[] datas,OutputStream out) throws IOException;

}

话费单上的每行记录的实现:

final public class Fee implements RowData{

private String mobile;

private String name;

private String password;

private float fee;

public Fee(String mobileNumber,String name,String password,float fee) {

setMobile(mobileNumber);

this.setName(name);

this.setPassword(password);

this.setFee(fee);

}

public Fee(){}

public int getColumns() {return 4;}

public String toString() {

return "mobile="+mobile + " name="+name + " password="+password+" fee="+fee;

}

public Object getDataAtColumn(int column) {

switch (column) {

case 0: return getMobile();

case 1: return getName();

case 2: return getPassword();

case 3: return getFee();

default:return null;

}

}

public String getMobile() {return mobile;}

public void setMobile(String mobile) {this.mobile = mobile;}

public String getName() {return name;}

public void setName(String name) {this.name = name;}

public String getPassword() {return password;}

public void setPassword(String password) {this.password = password;}

public float getFee() {return fee;}

public void setFee(float fee) {this.fee = fee;}

}

解析器的实现

import java.io.*;

import java.util.*;

final public class DefaultRowDataParser implements RowDataParser{

private final static int BUFFER_SIZE = 200;

private final static String SPLIT_STRING = " ";

public RowData[] parser(InputStream in) throws IOException {

InputStreamReader inReader = new InputStreamReader(in);

BufferedReader bufReader = new BufferedReader(inReader,BUFFER_SIZE);

ListRowData datas = new ArrayListRowData();

String s;

for (s = bufReader.readLine(); s!=null; ) {

Fee fee = new Fee();

String[] ds = s.split(SPLIT_STRING);

fee.setMobile(ds[0]);

fee.setName(ds[1]);

fee.setPassword(ds[2]);

fee.setFee(Float.parseFloat(ds[3]));

datas.add(fee);

s = bufReader.readLine();

}

bufReader.close();

inReader.close();

return datas.toArray(new RowData[]{});

}

public void save(RowData[] datas,OutputStream out) throws IOException {

OutputStreamWriter ow = new OutputStreamWriter(out);

BufferedWriter bufOut = new BufferedWriter(ow);

for (RowData rd : datas) {

for (int i = 0,j=rd.getColumns(); i j; i++) {

bufOut.write(rd.getDataAtColumn(i).toString());

bufOut.write(SPLIT_STRING);

}

bufOut.write("\r\n");

}

bufOut.close();

ow.close();

}

}

如何用java读取txt中的java文件,并且记录其中char,int,if,while等出现的个数

读取java文件就要用到io了,每一个单词应该会有分隔符,可能是空格,逗号import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class FileTest {

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException {

File file = new File(""/** 文件名 */

);

FileInputStream fis = new FileInputStream(file);

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String sb = "";

int count = 0;

sb = br.readLine();

while (sb != null) {

String[] array = sb.split(" "/** 文件分隔符 */

);

for (String string : array) {

if (string.equals("while")) {

count++;

}

}

sb = br.readLine();

}

System.out.println(count);

}

}

java中想记录文件的一部分行数是多少,该怎么做啊啊求大神指教啊,急急急!

if(!list.get(num0).equals(list.get(0))){

num++; //统计出每个结构有多少行

System.out.print("行数是"+num);

}else{

System.out.print("没有匹配项");

}

逻辑写错了,在想想吧! first 之后那些行都和 first 不相等的……

java记录文件的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java统计文件大小、java记录文件的信息别忘了在本站进行查找喔。