Java读写TXT文本
发布日期:2021-08-19 19:59:57 浏览次数:3 分类:技术文章

本文共 1851 字,大约阅读时间需要 6 分钟。

public String readTxtFile(String filePath) {		StringBuffer appInfolistInput = new StringBuffer();		try {			String encoding = "UTF8";			File file = new File(filePath);			if (file.isFile() && file.exists()) {				InputStreamReader read = new InputStreamReader(						new FileInputStream(file), encoding);				BufferedReader bufferedReader = new BufferedReader(read);				String lineTxt = null;				while ((lineTxt = bufferedReader.readLine()) != null) {					appInfolistInput.append(lineTxt);				}				read.close();				bufferedReader.close();			} else {				System.out.println("找不到指定的文件");			}		} catch (Exception e) {			System.out.println("读取文件内容出错");			e.printStackTrace();		}		return appInfolistInput.toString();	}

 

public void readByte(String fileName) {		InputStream is = null;		try {			is = new FileInputStream(fileName);			byte[] byteBuffer = new byte[is.available()];			int read = 0;			while((read = is.read(byteBuffer)) != -1){				System.out.write(byteBuffer, 0, read);			}		} catch (FileNotFoundException e) {						e.printStackTrace();		} catch (IOException e) {						e.printStackTrace();		}finally{						try {				if(is != null){					is.close();				}			} catch (IOException e) {								e.printStackTrace();			}		}			}

 

public void writeBuffer(String fileName){		try {			File file = new File(fileName);			BufferedWriter output = new BufferedWriter(new FileWriter(file));						output.write("hello wrold");			output.close();		} catch (IOException e) {			e.printStackTrace();		}	}

 

public void writeByte(String fileName){		try {			File file = new File(fileName);			OutputStream os = new FileOutputStream(file);			String s = "hello world";						byte[] byteBuffer = s.getBytes();			os.write(byteBuffer, 0, byteBuffer.length);			os.flush();			os.close();		} catch (IOException e) {			e.printStackTrace();		}	}

 

转载于:https://www.cnblogs.com/zhangfei/p/3992191.html

转载地址:https://blog.csdn.net/weixin_30951743/article/details/95893087 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Digital Roots
下一篇:HTML+css布局

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月23日 02时01分44秒