早教吧 育儿知识 作业答案 考试题库 百科 知识分享

求java大神指导Writeaprogramthatwillcheckatextfileforanumberofformattingandpunctuationmatters.Theprogramwillaskforanintput-filenameandanoutput-filenameandwillthencopyallthenextfromtheinputfiletotheoutputf

题目详情
求java大神指导
Write a program that will check a text file for a number of formatting and punctuation matters.The program will ask for an intput-file name and an output-file name and will then copy all the next from the input file to the output file,but with the following two changes:(1)Any string of two or more blank symbols is replaced by a single blank;(2)all sentences start with an uppercase letter.Define a sentence as beginning after a period,question mark,or exclamation mark followed by one or more whitespace characters.
▼优质解答
答案和解析
这个最好用正则表达式
思路首先拿到这个文件
然后输出流创建一个文件
读取文件
利用正则切分内容

已非问号,或感叹号后跟一个或多个空格字符为开始
问号,或感叹号后跟一个或多个空格字符为结束切分
一次.
将切分的结果首位如果是英文改为大写.
将处理后的结果再次用正则判断大于1个连续空格的替换为一个空格就可以了
下面的例子是读取文件和写文件.正则处理过程自己查查吧
try {
//测试URL
//URL url = new URL(getUrl()+"/UploadFiles/20121107/1352258400625.txt");
URL url = new URL(getUrl()+vambo.getFilePath());
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
File file = new File((getPath().substring(0,getPath().length()-1)+vambo.getFilePath().substring(0,vambo.getFilePath().lastIndexOf("/"))).replace("\\", "/"));
if(!file.exists()){
file.mkdir();
}
fs = new FileOutputStream((getPath().substring(0,getPath().length()-1)+vambo.getFilePath()).replace("\\", "/"));
byte[] buffer = new byte[1024];
int byteread = 0;
int bytesum = 0;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
fs.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally{
try {
fs.close();
} catch (IOException e) {
throw e;
}
}