Kotlin学习篇章_文件的读取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/likui19921224/article/details/80089664

先上代码:

/**
读取一个文件
*/
fun readFile(path:Path){
val s=Files.newInputStream(path);
try {
var byte=s.read();
while (byte!=-1){
print(byte);
byte=s.read();
}
}catch (e:IOException){
println(e.message);
}
finally {
s.close();
}
}

>

在kotlin中取消了new关键字 增加了类的主构函数:
范例:
class temple(a:int){
}
这里的(a:int)就是该类初始化必须初始化的主构函数
后面会介绍主构函数:
初始化主构函数范例: val temple=temple(1);
如果主构函数没有参数列表 那么默认为空主构函数 ;

源码:

public static InputStream newInputStream(Path path, OpenOption… options)
throws IOException
{
return provider(path).newInputStream(path, options);
}
//在java中我们是直接传入string 路径 但是kotlin中传入的path ; 这里给大家直接获取path的方法 <找了很久>paths.get(string:string)->path;

猜你喜欢

转载自blog.csdn.net/likui19921224/article/details/80089664