多种方式读文件内容。"/>
多种方式读文件内容。
一、多种方式读文件内容。
1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容
001 | import java.io.BufferedReader; |
003 | import java.io.FileInputStream; |
004 | import java.io.FileReader; |
005 | import java.io.IOException; |
006 | import java.io.InputStream; |
007 | import java.io.InputStreamReader; |
008 | import java.io.RandomAccessFile; |
009 | import java.io.Reader; |
010 | public class ReadFromFile { |
012 | * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 |
013 | * @param fileName 文件的名 |
015 | public static void readFileByBytes(String fileName){ |
016 | File file = new File(fileName); |
017 | InputStream in = null ; |
019 | System.out.println( "以字节为单位读取文件内容,一次读一个字节:" ); |
021 | in = new FileInputStream(file); |
023 | while ((tempbyte=in.read()) != - 1 ){ |
024 | System.out.write(tempbyte); |
027 | } catch (IOException e) { |
032 | System.out.println( "以字节为单位读取文件内容,一次读多个字节:" ); |
034 | byte [] tempbytes = new byte [ 100 ]; |
036 | in = new FileInputStream(fileName); |
037 | ReadFromFile.showAvailableBytes(in); |
039 | while ((byteread = in.read(tempbytes)) != - 1 ){ |
040 | System.out.write(tempbytes, 0 , byteread); |
042 | } catch (Exception e1) { |
043 | e1.printStackTrace(); |
048 | } catch (IOException e1) { |
054 | * 以字符为单位读取文件,常用于读文本,数字等类型的文件 |
055 | * @param fileName 文件名 |
057 | public static void readFileByChars(String fileName){ |
058 | File file = new File(fileName); |
059 | Reader reader = null ; |
061 | System.out.println( "以字符为单位读取文件内容,一次读一个字节:" ); |
063 | reader = new InputStreamReader( new FileInputStream(file)); |
065 | while ((tempchar = reader.read()) != - 1 ){ |
069 | if ((( char )tempchar) != '/r' ){ |
070 | System.out.print(( char )tempchar); |
074 | } catch (Exception e) { |
078 | System.out.println( "以字符为单位读取文件内容,一次读多个字节:" ); |
080 | char [] tempchars = new char [ 30 ]; |
082 | reader = new InputStreamReader( new FileInputStream(fileName)); |
084 | while ((charread = reader.read(tempchars))!=- 1 ){ |
086 | if ((charread == tempchars.length)&&(tempchars[tempchars.length- 1 ] != '/r' )){ |
087 | System.out.print(tempchars); |
089 | for ( int i= 0 ; i<charread; i++){ |
090 | if (tempchars[i] == '/r' ){ |
093 | System.out.print(tempchars[i]); |
099 | } catch (Exception e1) { |
100 | e1.printStackTrace(); |
105 | } catch (IOException e1) { |
111 | * 以行为单位读取文件,常用于读面向行的格式化文件 |
112 | * @param fileName 文件名 |
114 | public static void readFileByLines(String fileName){ |
115 | File file = new File(fileName); |
116 | BufferedReader reader = null ; |
118 | System.out.println( "以行为单位读取文件内容,一次读一整行:" ); |
119 | reader = new BufferedReader( new FileReader(file)); |
120 | String tempString = null ; |
123 | while ((tempString = reader.readLine()) != null ){ |
125 | System.out.println( "line " + line + ": " + tempString); |
129 | } catch (IOException e) { |
135 | } catch (IOException e1) { |
142 | * @param fileName 文件名 |
144 | public static void readFileByRandomAccess(String fileName){ |
145 | RandomAccessFile randomFile = null ; |
147 | System.out.println( "随机读取一段文件内容:" ); |
149 | randomFile = new RandomAccessFile(fileName, "r" ); |
151 | long fileLength = randomFile.length(); |
153 | int beginIndex = (fileLength > 4 ) ? 4 : 0 ; |
155 | randomFile.seek(beginIndex); |
156 | byte [] bytes = new byte [ 10 ]; |
160 | while ((byteread = randomFile.read(bytes)) != - 1 ){ |
161 | System.out.write(bytes, 0 , byteread); |
163 | } catch (IOException e){ |
166 | if (randomFile != null ){ |
169 | } catch (IOException e1) { |
178 | private static void showAvailableBytes(InputStream in){ |
180 | System.out.println( "当前字节输入流中的字节数为:" + in.available()); |
181 | } catch (IOException e) { |
186 | public static void main(String[] args) { |
187 | String fileName = "C:/temp/newTemp.txt" ; |
188 | ReadFromFile.readFileByBytes(fileName); |
189 | ReadFromFile.readFileByChars(fileName); |
190 | ReadFromFile.readFileByLines(fileName); |
191 | ReadFromFile.readFileByRandomAccess(fileName); |
二、将内容追加到文件尾部
001 | import java.io.FileWriter; |
002 | import java.io.IOException; |
003 | import java.io.RandomAccessFile; |
008 | public class AppendToFile { |
011 | * A方法追加文件:使用RandomAccessFile |
012 | * @param fileName 文件名 |
013 | * @param content 追加的内容 |
015 | public static void appendMethodA(String fileName, String content){ |
018 | RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw" ); |
020 | long fileLength = randomFile.length(); |
022 | randomFile.seek(fileLength); |
023 | randomFile.writeBytes(content); |
025 | } catch (IOException e){ |
030 | * B方法追加文件:使用FileWriter |
034 | public static void appendMethodB(String fileName, String content){ |
037 | FileWriter writer = new FileWriter(fileName, true ); |
038 | writer.write(content); |
040 | } catch (IOException e) { |
045 | public static void main(String[] args) { |
046 | String fileName = "C:/temp/newTemp.txt" ; |
047 | String content = "new append!" ; |
049 | AppendToFile.appendMethodA(fileName, content); |
050 | AppendToFile.appendMethodA(fileName, "append end. /n" ); |
052 | ReadFromFile.readFileByLines(fileName); |
054 | AppendToFile.appendMethodB(fileName, content); |
055 | AppendToFile.appendMethodB(fileName, "append end. /n" ); |
057 | ReadFromFile.readFileByLines(fileName); |
072 | public class FileOperate |
081 | public void newFolder(String folderPath) |
085 | String filePath = folderPath; |
086 | filePath = filePath.toString(); |
087 | File myFilePath = new File(filePath); |
088 | if (!myFilePath.exists()) |
092 | System.out.println( "新建目录操作 成功执行" ); |
096 | System.out.println( "新建目录操作出错" ); |
103 | public void newFile(String filePathAndName, String fileContent) |
107 | String filePath = filePathAndName; |
108 | filePath = filePath.toString(); |
109 | File myFilePath = new File(filePath); |
110 | if (!myFilePath.exists()) |
112 | myFilePath.createNewFile(); |
114 | FileWriter resultFile = new FileWriter(myFilePath); |
115 | PrintWriter myFile = new PrintWriter(resultFile); |
116 | String strContent = fileContent; |
117 | myFile.println(strContent); |
119 | System.out.println( "新建文件操作 成功执行" ); |
123 | System.out.println( "新建目录操作出错" ); |
130 | public void delFile(String filePathAndName) |
134 | String filePath = filePathAndName; |
135 | filePath = filePath.toString(); |
136 | File myDelFile = new File(filePath); |
138 | System.out.println( "删除文件操作 成功执行" ); |
142 | System.out.println( "删除文件操作出错" ); |
149 | public void delFolder(String folderPath) |
153 | delAllFile(folderPath); |
154 | String filePath = folderPath; |
155 | filePath = filePath.toString(); |
156 | File myFilePath = new File(filePath); |
157 | if (myFilePath.delete()) { |
158 | System.out.println( "删除文件夹" + folderPath + "操作 成功执行" ); |
160 | System.out.println( "删除文件夹" + folderPath + "操作 执行失败" ); |
165 | System.out.println( "删除文件夹操作出错" ); |
171 | * @param path String 文件夹路径 如 c:/fqf |
173 | public void delAllFile(String path) |
175 | File file = new File(path); |
180 | if (!file.isDirectory()) |
184 | String[] tempList = file.list(); |
186 | for ( int i = 0 ; i < tempList.length; i++) |
188 | if (path.endsWith(File.separator)) |
190 | temp = new File(path + tempList[i]); |
194 | temp = new File(path + File.separator + tempList[i]); |
200 | if (temp.isDirectory()) |
203 | delFolder(path+ File.separatorChar + tempList[i]); |
206 | System.out.println( "删除文件操作 成功执行" ); |
210 | * @param oldPath String 原文件路径 如:c:/fqf.txt |
211 | * @param newPath String 复制后路径 如:f:/fqf.txt |
213 | public void copyFile(String oldPath, String newPath) |
219 | File oldfile = new File(oldPath); |
223 | InputStream inStream = new FileInputStream(oldPath); |
224 | FileOutputStream fs = new FileOutputStream(newPath); |
225 | byte [] buffer = new byte [ 1444 ]; |
226 | while ( (byteread = inStream.read(buffer)) != - 1 ) |
229 | System.out.println(bytesum); |
230 | fs.write(buffer, 0 , byteread); |
234 | System.out.println( "删除文件夹操作 成功执行" ); |
238 | System.out.println( "复制单个文件操作出错" ); |
244 | * @param oldPath String 原文件路径 如:c:/fqf |
245 | * @param newPath String 复制后路径 如:f:/fqf/ff |
247 | public void copyFolder(String oldPath, String newPath) |
251 | ( new File(newPath)).mkdirs(); |
252 | File a= new File(oldPath); |
253 | String[] file=a.list(); |
255 | for ( int i = 0 ; i < file.length; i++) |
257 | if (oldPath.endsWith(File.separator)) |
259 | temp= new File(oldPath+file[i]); |
263 | temp= new File(oldPath+File.separator+file[i]); |
267 | FileInputStream input = new FileInputStream(temp); |
268 | FileOutputStream output = new FileOutputStream(newPath + "/" + |
269 | (temp.getName()).toString()); |
270 | byte [] b = new byte [ 1024 * 5 ]; |
272 | while ( (len = input.read(b)) != - 1 ) |
274 | output.write(b, 0 , len); |
280 | if (temp.isDirectory()) |
283 | copyFolder(oldPath+ "/" +file[i],newPath+ "/" +file[i]); |
286 | System.out.println( "复制文件夹操作 成功执行" ); |
290 | System.out.println( "复制整个文件夹内容操作出错" ); |
296 | * @param oldPath String 如:c:/fqf.txt |
297 | * @param newPath String 如:d:/fqf.txt |
299 | public void moveFile(String oldPath, String newPath) |
301 | copyFile(oldPath, newPath); |
306 | * @param oldPath String 如:c:/fqf.txt |
307 | * @param newPath String 如:d:/fqf.txt |
309 | public void moveFolder(String oldPath, String newPath) |
311 | copyFolder(oldPath, newPath); |
314 | public static void main(String args[]) |
317 | boolean exitnow= false ; |
318 | System.out.println( "使用此功能请按[1] 功能一:新建目录" ); |
319 | System.out.println( "使用此功能请按[2] 功能二:新建文件" ); |
320 | System.out.println( "使用此功能请按[3] 功能三:删除文件" ); |
321 | System.out.println( "使用此功能请按[4] 功能四:删除文件夹" ); |
322 | System.out.println( "使用此功能请按[5] 功能五:删除文件夹里面的所有文件" ); |
323 | System.out.println( "使用此功能请按[6] 功能六:复制文件" ); |
324 | System.out.println( "使用此功能请按[7] 功能七:复制文件夹的所有内容" ); |
325 | System.out.println( "使用此功能请按[8] 功能八:移动文件到指定目录" ); |
326 | System.out.println( "使用此功能请按[9] 功能九:移动文件夹到指定目录" ); |
327 | System.out.println( "使用此功能请按[10] 退出程序" ); |
330 | FileOperate fo= new FileOperate(); |
333 | BufferedReader Bin= new BufferedReader( new InputStreamReader(System.in)); |
334 | String a=Bin.readLine(); |
335 | int b=Integer.parseInt(a); |
338 | case 1 :System.out.println( "你选择了功能一 请输入目录名" ); |
342 | case 2 :System.out.println( "你选择了功能二 请输入文件名" ); |
344 | System.out.println( "请输入在" +aa+ "中的内容" ); |
348 | case 3 :System.out.println( "你选择了功能三 请输入文件名" ); |
352 | case 4 :System.out.println( "你选择了功能四 请输入文件名" ); |
356 | case 5 :System.out.println( "你选择了功能五 请输入文件名" ); |
360 | case 6 :System.out.println( "你选择了功能六 请输入文件名" ); |
362 | System.out.println( "请输入目标文件名" ); |
366 | case 7 :System.out.println( "你选择了功能七 请输入源文件名" ); |
368 | System.out.println( "请输入目标文件名" ); |
372 | case 8 :System.out.println( "你选择了功能八 请输入源文件名" ); |
374 | System.out.println( "请输入目标文件名" ); |
378 | case 9 :System.out.println( "你选择了功能九 请输入源文件名" ); |
380 | System.out.println( "请输入目标文件名" ); |
385 | System.out.println( "程序结束,请退出" ); |
387 | default :System.out.println( "输入错误.请输入1-10之间的数" ); |
389 | System.out.println( "请重新选择功能" ); |
393 | System.out.println( "输入错误字符或程序出错" ); |
地址:/
发布评论