为什么java ini文件读写中的file类无法只能操作不能读写?

当前访客身份:游客 [
当前位置:
今天碰到一个Java io流题目如下:
打开一个文件,文件的内容如下
要做的结果是在1和2这两行之间写入一行新的数据!!!
求问各位大神有多少中方法能写出来,最好是源代码写给我,谢谢!!
共有4个答案
<span class="a_vote_num" id="a_vote_num_
File file = new File(&D:\\dev\\workspace\\case\\src\\info.txt&);
RandomAccessFile raf = new RandomAccessFile(file, &rw&);
String oneline = raf.readLine();
String twoline = raf.readLine();
raf.seek(oneline.length()+ 2);
raf.write(&8888&.getBytes());
raf.write(&\r\n&.getBytes());
raf.write(twoline.getBytes());
raf.close();
<span class="a_vote_num" id="a_vote_num_
<span class="a_vote_num" id="a_vote_num_
帅哥,我没有api啊,在说我英文也不好,看不懂api
--- 共有 1 条评论 ---
API也有中文的,大兄弟
(1年前)&nbsp&
<span class="a_vote_num" id="a_vote_num_
自己看API,不谢。
更多开发者职位上
有什么技术问题吗?
学习者11的其他问题
类似的话题Eclipse中无法写入中文,以及页面显示java文件中的中文时,出现乱码_小组_ThinkSAAS
Eclipse中无法写入中文,以及页面显示java文件中的中文时,出现乱码
Eclipse中无法写入中文,以及页面显示java文件中的中文时,出现乱码
如果对单个文件:页面上Alt + Enter 查看属性,改为UTF-8就可以了
如果对整个工程:Window -& 属性 -& General -& Workspace,改为UTF-8就可以了
注:对原先用其它格式输入的中文可能都变成了乱码,这点还没有解决
用户评论(0)
开发技术学习小组列表
PHP开发框架
缓存Memcache
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
手机客户端
ThinkSAAS接收任何功能的Iphone(IOS)和Android手机的客户端定制开发服务
让ThinkSAAS更好,把建议拿来。Java中对文件进行操作的类都有哪些? - Java面试题 - 职友集(发现中国好公司)
<meta name="description" content="File类:
对硬盘上的文件和目录进行操作的类。
构造函数..." />
Java中对文件进行操作的类都有哪些?
阅读( 294 )
对硬盘上的文件和目录进行操作的类。
构造函数:
1) File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.
2)File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.
3)File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.
4)File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.
1) list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
list(FilenameFilter filter)
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
3) listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
4) listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
5) listFiles(FilenameFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
6) Boolean
createNewFile()
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
Creates the directory named by this abstract pathname.
8) static File createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
File path = new File(&#8220;icons&#8221;);
try &#123;
//创建文件和目录
path.mkdir();
File.createTempFile(&#8220;temp&#8221;,&#8221;.java&#8221;,path);
&#125; catch (IOException e) &#123;
System.out.println(&#8220;创建临时文件异常:= &#8221; +e.getMessage());
File f = new File(&#8220;.&#8221;);
File[] fileList = f.listFiles();
for(int i = 0; i & fileList.i++)&#123;
if(fileList[i].isDirectory())&#123;
System.out.println(&#8220;目:= &#8221; + fileList[i].getAbsolutePath());
&#125;else&#123;
System.out.println(&#8220;文:= &#8221; + fileList[i].getAbsolutePath());
//筛选文件
String[] fileNames = f.list(new FilterNames());
for(int i = 0; i & fileNames.i++)&#123;
System.out.println(&#8220;名称为:= &#8221; + fileNames[i]);
流的分类:
1)节点流:应用程序直接操作目标设备所对应的流,只能操作字节字符型数据。
InputStream
OutputStream
2)包装流:提供了应用程序与外部设备之间,各种类型数据的读写功能。
对文件操作的流:
FileInputStream
FileOutputStream
FileReader
FileWriter
RandomAccessFile
RandomAccess
File f = new File(&#8220;configuration.txt&#8221;);
InputStream in =
OutputStream out =
String data = &#8220;世界和平是任何力量阻挡不了的&#8221; + System.getProperty(&#8220;line.separator&#8221;);
//调用系统的换行方式,在windows中使用&#8217;&#92;r&#92;n&#8217;
try &#123;
out = new FileOutputStream(f,true);
//追加在文件的末尾的方法
out.write(data.getBytes());
out.close();
&#125; catch (IOException e) &#123;
System.out.println(&#8220;写文件数据异常:= &#8221; + e.getMessage());
in = new FileInputStream(f);
byte[] buff = new byte[102];
while((len = in.read(buff)) != -1)&#123;
System.out.println(&#8220;len:= &#8221; + len);
System.out.println(new String(buff,0,len));
in.close();
&#125;catch(IOException e)&#123;
System.out.println(&#8220;读取文件数据异常:= &#8221; + e.getMessage());
分享给朋友:
亲~ 如果您有更好的答案 可在评论区发表您独到的见解。
您想查看更多的信息:
Java面试题目推荐
微信公众号java中同时对一个文件建立读写流时,为什么有时候出现问题呢
[问题点数:30分,结帖人w]
java中同时对一个文件建立读写流时,为什么有时候出现问题呢
[问题点数:30分,结帖人w]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
相关推荐:
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 java文件读写 的文章

 

随机推荐