对Java IO的一些总结 (3)
注:文中使用部分方法请参考《对Java IO的一些总结 (1) 》及《对Java IO的一些总结 (2) 》读文件的关键技术点如下:
1. 用FileInputStream打开文件输入流,通过read方法以字节为单位读取文件,是最通用的读文件的方法,能读取任何文件,特别适合读二进制文件,如图片、声音、视频文件。
2. 用InputStreamReader打开文件输入流,通过read方法以字符为单位读取文件,常用于读取文本文件
3. 用BufferedReader打开文件输入流,通过readLine方法以行为单位读取文件,重用于读格式化的文本。
4. 用RandomAccessFile打开文件输入流,通过seek方法将读指针移到文件内容中间,
再通过read方法读取指针后文件内容,常用于随机的读取文件。
注意:文件读取结束后,关闭流。
/** * 以字节为单位读取文件(一次读一个字节) * 常用于读取二进制文件。如图片,声音,影像等文件 * @param filePath 文件名 */public static String readFileByBytesInOneByOne(String filePath) {File file = new File(filePath);InputStream in = null;StringBuffer sub = new StringBuffer();try {in = new FileInputStream(file); //一次读一个字节int tempbyte;while ((tempbyte = in.read()) != -1) {sub.append((char)tempbyte);}in.close();} catch (IOException e) {e.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException el) {el.printStackTrace();}}}return sub.toString();}/** * 以字节为单位读取文件(一次读多个字节) * 常用于读取二进制文件。如图片,声音,影像等文件 * @param filePath 文件名 */public static void readFileByBytes(String filePath) {File file = new File(filePath);InputStream in = null;StringBuffer sub = new StringBuffer();try {byte[] tempbytes = new byte;int byteread = 0;in = new FileInputStream(file);showAvailableBytes(in);while ((byteread = in.read(tempbytes)) != -1) {System.out.write(tempbytes, 0, byteread);}} catch (Exception el) {el.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException el) {el.printStackTrace();}}}System.out.println(sub.toString());}
/** * 以字符为单位读取文件,常用于读文本、数字等类型的文件(一次读一个字符) * @param filepath 文件名 */public static String readFileByCharsInOneByOne(String filepath) {File file = new File(filepath);Reader reader = null;StringBuffer sub = new StringBuffer();try {reader = new InputStreamReader(new FileInputStream(file));int tempchar;while ((tempchar = reader.read()) != -1) {/* 在windows下,这两个字符在一起时,表示一个换行,但如果这两个字符分开显示时 * 会换两次行,因此,屏蔽掉 ,或者;否则,将会多出很多空行 */if (((char) tempchar) != ' ') {sub.append((char) tempchar);}}reader.close();} catch (Exception e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {e1.printStackTrace();}}}return sub.toString();}/** * 以字符为单位读取文件,常用于读文本、数字等类型的文件(一次读多个字符) * @param filepath 文件名 */public static void readFileByChars(String filepath) {File file = new File(filepath);Reader reader = null;try {char[] tempchars = new char;int charread = 0;reader = new InputStreamReader(new FileInputStream(file));while ((charread = reader.read(tempchars)) != -1) {if ((charread == tempchars.length) && (tempchars != ' ')) {System.out.print(tempchars);} else {for (int i = 0; i < charread; i++) {if (tempchars == ' ') {continue;} else {System.out.print(tempchars);}}}}} catch (Exception e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {e1.printStackTrace();}}}}/** * 以行为单位读取文件,常用于读取面向行的格式化文件 * @param fileName 文件名 */public static String readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;StringBuffer sub = new StringBuffer();try {reader = new BufferedReader(new FileReader(file));String tempString = null;//int line = 1;while ((tempString = reader.readLine()) != null) {//System.out.println("line:" + line + ": " + tempString); //显示行号//line++;sub.append(tempString+"\n");}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {e1.printStackTrace();}}}return sub.toString();}
用java写文件有多种方法,对于不同类型的数据,有不同的写入方法,写文件的关键技术点如下:
1. FileOutputStream打开文件输出流,通过write方法以字节为单位写入文件,是写文件最通用的方法,能写入任何类型的文件,特别适合写二进制数据文件。
2. OutputStreamWriter打开文件输出流,通过write方法以字符为单位写入文件,能够将字符数组和字符串写入文件。
3. PrintWriter打开文件输出流,通过print和println方法写字符串到文件,与System.out的用法相似,常用于写入格式化的文本。
注意:当文件写完后关闭输出流。
/** * 以字节为单位写文件。适合于写二进制文件,如图片等 * @param filePath 文件名 */ public static void writeFileByBytes(String filePath, String content) { File file = new File(filePath); OutputStream out = null; try { out = new FileOutputStream(file); byte[] bytes = content.getBytes(); //读取输出流中的字节 out.write(bytes); System.out.println("Write \"" + file.getAbsolutePath() + "\" success"); } catch (IOException e) { System.out.println("Write \"" + file.getAbsolutePath() + "\" fail"); e.printStackTrace(); } finally { if (out != null) { try { out.close(); } catch(IOException e1) { e1.printStackTrace(); } } } } /** * 以字符为单位写文件 * @param fileName 文件名 */ public static void writeFileByChars(String fileName, String content) { File file = new File(fileName); Writer writer = null; try { //打开文件输出流 //OutputStreamWriter是字符流通向字节流的桥梁:使用指定的charset将要向其写入的字符编码为字节 writer = new OutputStreamWriter(new FileOutputStream(file)); writer.write(content); System.out.println("Write \"" + file.getAbsolutePath() + "\" success."); } catch (IOException e) { System.out.println("Write \"" + file.getAbsolutePath() + "\" fail."); e.printStackTrace(); } finally { if(writer != null) { try{ writer.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } /** * 以行为单位写文件 * @param fileName 文件名 */ public static void writeFileByLines(String fileName, String content) { File file = new File(fileName); PrintWriter writer = null; try { writer = new PrintWriter(new FileOutputStream(file)); writer.println(content); //写字符串 //写入各种基本类型数据 writer.print(true); writer.print(155); writer.println(); //换行 writer.flush(); //写入文件 System.out.println("Write \"" + file.getAbsolutePath() + "\" success."); } catch (IOException e) { System.out.println("Write \"" + file.getAbsolutePath() + "\" fail."); e.printStackTrace(); } finally { if (writer != null) { writer.close(); //关闭输出文件流 } } }
向文件尾追加内容有多种方法,下面介绍两种常用的方法。具体如下:
1. 通过RandomAccessFile以读写的方式打开文件输出流,使用它的seek方法可以将读写指针移到文件尾,再使用它的write方法将数据写道读写指针后面,完成文件追加。
2. 通过FileWriter打开文件输出流,构造FileWriter时指定写入模式,是一个布尔值,为true时表示写入的内容添加到已有文件内容的后面,为false时重新写文件,以前的数据被清空,默认为false。
/** * A方法追加文件。使用RandomAccessFile * @param fileName 文件名 * @param content 追加的内容 */public static void appendFileContent_I(String fileName, String content) {try {//按读写方式打开一个随机访问文件流RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");long fileLength = randomFile.length(); //文件长度,字节数randomFile.seek(fileLength); //将写文件指针移到文件尾//randomFile.writeBytes(content); //中文产生乱码randomFile.write(content.getBytes()); //中文不产生乱码randomFile.close();} catch (IOException e) {e.printStackTrace();}}/** * B方法追加文件。使用FileWriter * @param fileName 文件名 * @param content 追加的内容 */public static void appendFileContent_II(String fileName, String content) {try {//打开一个写文件器,构造函数的第二个参数true表示以追加的形式写文件FileWriter writer = new FileWriter(fileName, true);writer.write(content);writer.close();} catch (IOException e) {e.printStackTrace();}}
页:
[1]