好文档 - 专业文书写作范文服务资料分享网站

Java流(文件读写操作)

天下 分享 时间: 加入收藏 我要投稿 点赞

/**

* 以字符为单位读取文件,常用于读文本,数字等类型的文件 * @param fileName:文件名 */

public static void readFileByChars(String fileName) {

Reader reader = null; try {

logger.debug(\以字符为单位读取文件内容,一次读多个字节:\); /*一次读多个字符*/

char[] tempchars = new char[100]; int charread = 0;

if(fileName!=null&&!\.equals(fileName)){ }

reader = new InputStreamReader(new

/*读入多个字符到字符数组中,charread为一次读取字符数*/ while ((charread = reader.read(tempchars)) != -1) { }

/*对于windows下,rn这两个字符在一起时,表示一个换行。*/ /*但如果这两个字符分开显示时,会换两次行。*/

/*因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。*/ if ((charread == tempchars.length) }

&& (tempchars[tempchars.length - 1] != 'r')) { logger.debug(tempchars);

for (int i = 0; i < charread; i++) { }

if (tempchars[i] == 'r') { }

continue;

logger.debug(tempchars[i]); } else {

FileInputStream(fileName));

} else {

} catch (Exception e1) {

logger.error(\读取文本文件异常\,e1); } finally { }

if (reader != null) { }

try {

reader.close();

} catch (IOException e1) {

logger.error(\读取文本文件异常\,e1); }

} /**

* 以行为单位读取文件,常用于读面向行的格式化文件 * @param fileName:文件名 */

public static List readFileByLines(String fileName) { } }

return list;

List list = new ArrayList(); if(fileName!=null&&!\.equals(fileName)){ File file = new File(fileName); BufferedReader reader = null; try {

logger.debug(\以行为单位读取文件内容,一次读一整行:\); reader = new BufferedReader(new FileReader(file)); String tempString = null;

/*一次读入一行,直到读入null为文件结束*/

while ((tempString = reader.readLine()) != null) { }

logger.debug(tempString); list.add(tempString);

} catch (IOException e) {

logger.error(\读取文本文件异常\,e); } finally { }

if (reader != null) { }

try {

reader.close();

} catch (IOException e1) {

logger.error(\读取文本文件异常\,e1); }

5.2、常用写文件:

/**

* 把内容写到文件

* @param filePathName 文件名 * @param List 文件内容 */

public static boolean writerFile(String filePathName,String }

boolean flag=false;

OutputStreamWriter osw=null; try { }

if(osw!=null){

BufferedWriter bw=new BufferedWriter(osw); try { } }

return flag;

if(content!=null&&!\.equals(content)){ }

flag=false;

e.printStackTrace(); try {

bw.close(); osw.close(); flag=false;

e.printStackTrace();

bw.write(content); flag= true;

if(filePathName!=null&&!\.equals(filePathName)){ }

flag=false;

e1.printStackTrace();

osw = new OutputStreamWriter(new

content){

FileOutputStream(filePathName));

} catch (FileNotFoundException e1) {

} catch (IOException e) {

}finally{

} catch (IOException e) {

}

/** * 把内容写到文件或追加到文件中 * @param filePathName 文件名 * @param List 文件内容 */

public static boolean writerFileIsAppend(String filePathName,String content){

boolean flag=false;

OutputStreamWriter osw=null; try { if (filePathName!=null&&!\.equals(filePathName)) { osw = new OutputStreamWriter(new

FileOutputStream(filePathName,true));

}

} catch (Exception e1) {

flag=false;

e1.printStackTrace();

}

if(osw!=null){

BufferedWriter bw=new BufferedWriter(osw); try { if(content!=null&&!\.equals(content)){ bw.write(content); flag= true;

}

} catch (IOException e) {

flag=false;

e.printStackTrace(); }finally{

try { bw.close(); osw.close(); } catch (IOException e) {

flag=false;

e.printStackTrace();

}

} }

return flag;

}

六、 RandomAccessFile

6.1:说明

? RandomAccessFile是一种特殊的文件流,可以用它在文件的任何地方查找或者插入数据

? RandomAccessFile同时实现了DataInput和DataOutput接口,所以可以用它来读/写文件 ? 构造器:

---RandomAccessFile(java.io.File f,String mode) ---RandomAccessFile(String file,String mode)

6.2:代码示例

/**

* Description : 读取文件最后一行内容 * @param fileName 文件路径名+文件名 */

public static String getfinalLineData(String pathName){ RandomAccessFile raf = null; String lastLine = \;

try {

raf = new RandomAccessFile(pathName, \); long len = raf.length(); if (len != 0L) { }

e.printStackTrace();

long pos = len - 1; while (pos > 0) { }

pos--;

raf.seek(pos);

if (raf.readByte() == '\\n') { }

lastLine = raf.readLine(); break;

} catch (Exception e) { }finally {

Java流(文件读写操作)

/***以字符为单位读取文件,常用于读文本,数字等类型的文件*@paramfileName:文件名*/publicstaticvoidreadFileByChars(StringfileName){
推荐度:
点击下载文档文档为doc格式
2acr74vb9j5dq8n1sig30fluh9boav00ujt
领取福利

微信扫码领取福利

微信扫码分享