文件的输入输出的应用
2016-05-04 19:37
295 查看
1.使用字符输出流将一段文字存入文件,然后使用输入流读取文件中的内容。
2.将字符串加密后写入文件,然后读取该文件,并解密。
public class main { public static void main(String[] args) { File file = new File("hello.txt"); String s; Scanner r = new Scanner(System.in); s = r.nextLine(); char b[] = s.toCharArray(); try { FileWriter out = new FileWriter(file, true); out.write(b); out.close(); FileReader in = new FileReader(file); int n = 0; while ((n = in.read(b, 0, 2)) != -1) { String str = new String(b, 0, n); System.out.print(str); } } catch (IOException e) { System.out.println(e); } } }
2.将字符串加密后写入文件,然后读取该文件,并解密。
public class Main { public static void main(String[] args) { File file = new File("hello.txt"); String s; Scanner r = new Scanner(System.in); s = r.nextLine(); char b[] = s.toCharArray(); for (int i = 0; i < b.length; i++) { if (b[i] >= 'x') b[i] -= 23; else b[i] += 3; } try { FileWriter out = new FileWriter(file, true); out.write(b); out.close(); FileReader in = new FileReader(file); int n = 0; while ((n = in.read(b, 0, 1)) != -1) { String str = new String(b, 0, n); char x[] = str.toCharArray(); if (x[0] <= 'c') System.out.print((char)(x[0] + 23)); else System.out.print((char)(x[0] - 3)); } } catch (IOException e) { System.out.println(e); } } }
相关文章推荐
- Java 初学者学习路线(浓缩版)
- 推荐喜欢算法
- ubuntu系统设置问题
- html第二天div布局盒模型概念
- mysql报错处理
- java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger.log 造成的原因和解决方法
- App Extension编程指南(iOS8/OS X v10.10)中文版
- 在项目中使用了tomcat的数据库连接池的配置过程
- springmvc在业务控制方法中收集List<JavaBean>参数,实现批量处理
- 用PHP来AC(PHP : PHP 5.3.28)
- 云平台上配置scrapy的环境
- 自己看看
- POJ 1122 FDNY to the Rescue!(最短路径)
- HDU 4734 F(x) 数位 dp
- docker 私有仓库问题
- 《剑指offer》-矩形覆盖
- HDU 2829 Lawrence(DP+四边形不等式优化)
- java之代理设计模式
- Java ConcurrentModificationException异常原因和解决方法
- button的OnClickListener的三种实现方法