java/kotlin 读取文件、写入文件
package dh.btb.backend.utils
import java.io.*object FileUtil { /**
* 创建文件
* @param filePath 文件路径(不要以/结尾)
* @param fileName 文件名称(包含后缀,如:ReadMe.txt)
* @throws IOException
*/
@Throws(IOException::class)
fun createTxtFile(filePath: String, fileName: String): Boolean {
var flag = false
val filename = File("$filePath/$fileName")
if (!filename.exists()) {
filename.createNewFile()
flag = true
}
return flag
} /**
* 写文件
*
* @param content 文件内容
* @param filePath 文件路径(不要以/结尾)
* @param fileName 文件名称(包含后缀,如:ReadMe.txt)
* 新内容
* @throws IOException
*/
fun writeTxtFile(content: String, filePath: String, fileName: String, append: Boolean): Boolean {
var flag: Boolean = true
val thisFile = File("$filePath/$fileName")
try {
if (!thisFile.parentFile.exists()) {
thisFile.parentFile.mkdirs()
}
val fw = FileWriter("$filePath/$fileName", append)
fw.write(content)
fw.close()
} catch (e: IOException) {
e.printStackTrace()
}
return flag
} /**
* 读TXT文件内容
* @param filePath 文件路径(不要以 / 结尾)
* @param fileName 文件名称(包含后缀,如:ReadMe.txt)
* @return
*/
@Throws(Exception::class)
fun readTxtFile(filePath: String, fileName: String): String? {
var result: String? = ""
val fileName = File("$filePath/$fileName")
var fileReader: FileReader? = null
var bufferedReader: BufferedReader? = null
try {
fileReader = FileReader(fileName)
bufferedReader = BufferedReader(fileReader)
try {
var read: String? = null
while ({ read = bufferedReader.readLine();read }() != null) {
result = result + read + "\r\n"
}
} catch (e: Exception) {
e.printStackTrace()
} } catch (e: Exception) {
e.printStackTrace()
} finally {
if (bufferedReader != null) {
bufferedReader.close()
}
if (fileReader != null) {
fileReader.close()
}
}
println("读取出来的文件内容是:\r\n$result")
return result
}
} fun main(args: Array<String>) {
val service = FileUtil
val pathName = "E:/temp"
val fileName = "ReadMe.json"
val content = "我现在在上班" +
"比较忙的时候别来打扰我"
service.createTxtFile(pathName, fileName)
service.writeTxtFile(content, pathName, fileName, false)
val str = service.readTxtFile(pathName, fileName)
println(str)
}
java/kotlin 读取文件、写入文件的更多相关文章
- java nio读取和写入文件
读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...
- 读取和写入 文件 (NSFIleManger 与 NSFileHandle)
读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [su ...
- File类的特点?如何创建File类对象?Java中如何操作文件内容,什么是Io流Io流如何读取和写入文件?字节缓冲流使用原则?
重难点提示 学习目标 1.能够了解File类的特点(存在的意义,构造方法,常见方法) 2.能够了解什么是IO流以及分类(IO流的概述以及分类) 3.能够掌握字节输出流的使用(继承体系结构介绍以及常见的 ...
- Java中读取properties资源文件
一.通过ResourceBundle来读取.properties文件 /** * 通过java.util.resourceBundle来解析properties文件. * @param String ...
- 在线程中进行读取并写入文件和wenjia
新人求(胸)罩!!! import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException ...
- Python读取和写入文件
1 从文件中读取数据 1.1 读取整个文件 创建名为test的txt文本文件,添加内容如下所示: 123456789023456789013456789012 实现代码: with open('tes ...
- 【转】MFC中用CFile读取和写入文件2
原文网址:http://blog.sina.com.cn/s/blog_623a7fa40100hh1u.html CFile提供了一些常用的操作函数,如表1-2所示. 表1-2 CFile操作函数 ...
- C# Byte[]数组读取和写入文件
这个项目我用的是asp.net构建的,代码如下 protected void ByteToString_Click(object sender, EventArgs e) { string conte ...
- iOS学习 plist读取和写入文件
干iOS开发时间.后经常用来plist文件, 那plist什么文件是它? 它的全称是:Property List.属性列表文件.它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.plis ...
随机推荐
- iOS开发之--蓝牙开发实战
转载自:http://www.cnblogs.com/zyjzyj/p/6029968.html ,感谢英杰 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文 ...
- sass学习之一:sass安装compass部署
主要参考 http://www.jianshu.com/p/5bfc9411f58f -------------------------------------------- sass基于ruby 需 ...
- 巨蟒python全栈开发flask9 项目开始1
1.项目需求分析 立项:Javis&&taisen(三个月全部,先模拟出一个玩具,硬件需要周期长一些) 想法 --- 需求分析: .通过玩具与孩子实时进行沟通 .希望玩具的知识渊博 . ...
- 安装Vmware ESX Server5.5 ——hardware virtualization is not a feature of the cpu or is not enabled in the BIOS
Error信息: hardware virtualization is not a feature of the cpu or is not enabled in the BIOS 解决方案: F2进 ...
- tableView的使用(一)
//tableView的创建 //1.初始化 initWithFrame:style:(plian,gronp) //2.设置属性(行高, 分割线, 表头, 表尾) //3.添加到父 ...
- py, pyc, pyw, pyo, pyd 及发布程序时的选择 Compiled Python File (.pyc)
Python 程序扩展名(py, pyc, pyw, pyo, pyd)及发布程序时的选择 - 司开星的专栏 - CSDN博客 https://blog.csdn.net/chroming/artic ...
- event chrome firefox 获取点击对象的 id 类
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- Vulnerabilities by Type
w http://hackergossips.com/cross-site-scriptingxss-and-preventing/
- ArcGIS runtime for wpf 部署
简介 ArcGIS runtime for wpf 是一个轻量级的产品,部署方便,甚至可以做到直接部署在U盘中,做到即插即用. 部署一般需要遵循如下几个步骤: 1. 设定部署许可 虽然ArcGIS r ...
- scrapy spider
spider 定义:在spiders文件夹中由用户自定义,继承scrapy.Spider类或其子类 Spider并没有提供什么特殊的功能. 其仅仅请求给定的 start_urls/start_requ ...