1文件读入输出最长单词链
设计思路:文件读取输出之前都有练习过,这个主要是用到一个首尾字母的比较和单词链总数的比较等。第二个用到if的判断语句来进行异常处理
package dancilian;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class dancilian {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
String filename ="F:/input.txt";
File a=new File(filename);
//judeFileExists(a);
if(judeFileExists(a))
{
danci(filename);
}
else
{}
}
public static void danci(String s) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s));
StringBuffer sb = new StringBuffer();
String text = null;
while ((text = br.readLine()) != null) {
sb.append(text);// 将读取出的字符追加到stringbuffer中
}
br.close(); // 关闭读入流
String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
StringBuffer yao = new StringBuffer();
String b1=words[0];
yao.append(b1);
yao.append(" ");
String end=b1.substring(b1.length()-1,b1.length());
for(int i=1;i<words.length;i++)
{
String start=words[i].substring(0,1);
if(end.equals(start))
{
end=words[i].substring(words[i].length()-1,words[i].length());
yao.append(words[i]);
yao.append(" ");
}
}
File file =new File("F:/output.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw =new FileWriter(file);
fw.write(yao.toString());
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
// 判断文件是否存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
System.out.println("该文件存在");
return true;
} else {
System.out.println("该文件不存在");
return false;
}
}
}


2 进行异常判断
public static void main(String[] args) throws IOException
{
// TODO 自动生成的方法存根
String filename ="input1.txt";
File a=new File(filename);
File file=new File("output1.txt");
long startTime = System.currentTimeMillis();
//judeFileExists(a);
if(judeFileExists(a))
{
danci(filename);
}
else
{
try {
FileWriter fw =new FileWriter(file);
fw.write("文件不存在");
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis(); //获取结束时间
}
public static void danci(String s) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(s));
StringBuffer sb = new StringBuffer();
String text = null;
while ((text = br.readLine()) != null) {
sb.append(text); // 将读取出的字符追加到stringbuffer中
}
br.close(); // 关闭读入流
String str = sb.toString().toLowerCase(); // 将stringBuffer转为字符并转换为小写
String[] words = str.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
StringBuffer yao = new StringBuffer();
String b1=words[0];
yao.append(b1);
yao.append(" ");
if(b1.equals(""))
{
System.out.println("文件为空");
yao.append("文件为空");
}
else
{
if(words.length==1)
{
System.out.println("该文件只有一个单词");
}
String end=b1.substring(b1.length()-1,b1.length());
// System.out.println(end);
for(int i=1;i<words.length;i++)
{
String start=words[i].substring(0,1);
if(end.equals(start))
{
if(judechong(yao,words[i]))
{}
else
{
end=words[i].substring(words[i].length()-1,words[i].length());
yao.append(words[i]);
yao.append(" ");
}
}
}
if(yao.toString().equals(words[0]+" "))
{
yao.append("无互联语句");
System.out.println("无互联词");
}
}
File file =new File("output2.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter fw =new FileWriter(file);
fw.write(yao.toString());
fw.flush();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static boolean judechong(StringBuffer yao,String word)
{
String a=yao.toString();
boolean flag=false;
String[] words = a.split("[^(a-zA-Z)]+"); // 非单词的字符来分割,得到所有单词
for(int i=0;i<words.length;i++)
{
if(word.equals(words[i]))
{
flag=true;
}
}
return flag;
}
// 判断文件是否存在
public static boolean judeFileExists(File file) {
if (file.exists()) {
System.out.println("文件存在");
return true;
} else {
System.out.println("文件不存在");
return false;
}
}
// 判断文件夹是否存在(未修改)
public static void judeDirExists(File file) {
if (file.exists()) {
if (file.isDirectory()) {
System.out.println("dir exists");
} else {
System.out.println("the same name file exists, can not create dir");
}
} else {
System.out.println("dir not exists, create it ...");
file.mkdir();
}
}
}