编程一
来源:互联网 发布:匿名者专用黑页源码 编辑:程序博客网 时间:2024/06/13 09:12
题目:编写程序,将a.txt 文件中的单词与b.txt文件中的单词交替合并到 c.txt文件中,a.txt文件中的单词用回车符分离,b.txt文件中用回车或空格进行分离
//首先,新建一个文件Txt,其中包含 a.txt、b.txt、c.txt
package com.test1;import java.io.FileWriter;/* * 编写程序,将a.txt 文件中的单词与b.txt文件中的单词交替合并到 c.txt文件中, * a.txt文件中的单词用回车符分离,b.txt文件中用回车或空格进行分离 */public class Mainclass { public static void main(String[] args) throws Exception { FileManager a = new FileManager("./Txt/a.txt", new char[] { '\n' }); FileManager b = new FileManager("./Txt/b.txt", new char[] { '\n', ' ' }); FileWriter c = new FileWriter("./Txt/c.txt"); String aWord = null; String bWord = null; while ((aWord = a.nextWord()) != null) { c.write(aWord + "\n"); bWord = b.nextWord(); if (bWord != null) { c.write(bWord + "\n"); } c.close(); } }}
package com.test1;import java.io.File;import java.io.FileReader;public class FileManager { String[] words = null; int pos = 0; public FileManager(String filename, char[] seperators) throws Exception { File f = new File(filename); FileReader fileReader = new FileReader(f); char[] buf = new char[(int) f.length()]; // f.length 获取文件的大小 并用int强制转换 int len = fileReader.read(buf); String results = new String(buf, 0, len); String regex = null; if (seperators.length > 1) { regex = "" + seperators[0] + "|" + seperators[1]; } else { regex = "" + seperators[0]; } words = results.split(regex); } public String nextWord() { if (pos == words.length) return null; return words[pos++]; }}
0 0
- 编程一
- Unix 编程:多线程编程(一)
- 注册表编程扫盲(一)
- J2EE编程起步(一)
- OCI编程(一)
- Win32编程心得。一
- J2EE编程起步(一)
- Linux编程学习一
- Bash基本编程(一)
- Windows网络编程一
- 编程修养(一)(zz)
- 流媒体编程(一)
- shell编程笔记一
- VESA 编程介绍(一)
- 编程修养(一)
- 编程修养(一)
- Linux网络编程(一)
- socket编程原理(一)
- android APK瘦身大法——SVG图片瘦身
- Electron入门笔记:安装篇
- 五个案例让你明白GCD死锁
- Android Studio的下载、安装与配置
- 关于高德地图标注的那些坑
- 编程一
- 数据库笔记(数据基本概念)
- OC转Swift之基础篇(三)--类
- Android5.0之Toobar的使用
- [Android四大组件] Broadcast Receiver 广播
- Wolf从零学编程-用Python打造简单加密程序(一)
- Android GridView 选择item 放大的动画
- Java中的四种引用
- CTreeCtrl Class Windows 公共树视图控件的功能。