早教吧作业答案频道 -->其他-->
求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.
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;
}
}
思路首先拿到这个文件
然后输出流创建一个文件
读取文件
利用正则切分内容
已非问号,或感叹号后跟一个或多个空格字符为开始
问号,或感叹号后跟一个或多个空格字符为结束切分
一次.
将切分的结果首位如果是英文改为大写.
将处理后的结果再次用正则判断大于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;
}
}
看了 求java大神指导Write...的网友还看了以下:
按字母拼单词1、M-F-R-R-E-A2、V-T-E-S3、O-S-E-M-U4、S-L-U-E- 2020-05-16 …
设R+表示正实数集,求函数f:R+----->R+,是的对所有x,y属于R+,有f(xf(y))= 2020-06-12 …
函数f:R—R',满足f(0)=1,且对任意x,y属于R,均有f(xy+1)=f(x)*f(y)- 2020-06-16 …
已知函数f:R->R,x->3x-5,求x=2、5、8时的象f(2),(5),(8)已知函数f:R 2020-06-20 …
求解一个6阶的方阵R,满足条件:R*R'=diag(1,1,1,1,1,1)并且[0.0535,0 2020-06-22 …
完全平方式问题R'/(R+R')^2求最大值,把下面的平方式展开变成R'/(R^2+2RR'+R' 2020-06-27 …
英语单词填空1.时间状语:d-r-n-2.场所:b-s-s-o-f-r--e-a-t-e-t3.教 2020-07-14 …
试求出所有的函数f:R→R,使得对于任何的x,y∈R,都有f(x^2+y^2)=xf(x)+yf(y 2020-10-31 …
已知R={A,B,C,D},F={AB→C,C→A,C→D},确定R的候选码和主码。下面是老师给出的 2020-11-02 …
这几个字母怎么拼才是单词呢?B-E-E-N-M-O-R-VA-E-R-T-WA-B-C-D-O-P- 2021-02-09 …