2.27号今日总结
今天下午上的软件工程,一直对课上练习进行编译改正
代码如下:
package basic;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
public class Max {
private static int letterBack(String letter){
char[] res = letter.toCharArray();
return (res[res.length-1]-'a');
}
private static int letterFront(String letter){
char[] res = letter.toCharArray();
return (res[0]-'a');
}
public static void main(String[] args) throws IOException {
// FileInputStream file = new FileInputStream("E:\\workspace\\untitled\\src\\main\\java\\com\\atguigu\\git\\makr.txt");
// InputStreamReader reader = new InputStreamReader(file);
// BufferedReader bufferedReader = new BufferedReader(reader);
// String temp = "";
// while ((temp=bufferedReader.readLine())!=null){
// System.out.println(temp);
// }
// bufferedReader.close();
String ph = "D:\\IDEA项目\\input1.txt";
if(!new File(ph).exists()){
System.out.println("文件路径不存在");
return;
}
Path path = Paths.get(ph);
byte[] data = Files.readAllBytes(path);
String result = new String(data, StandardCharsets.UTF_8);
result = result.replaceAll("[^a-zA-Z0-9\\u4e00-\\u9fa5]"," ");
// System.out.println(result);
int lastLetter = -1;
int currLength = 0;
int MaxLength = 0;
String[] results = result.split(",|.|,|。");
int cur = 0;
String[] others = result.split(" ");
for (int i = 0; i < others.length; i++) {
String str = others[i];
if (str.equals("")) continue;
if(str.length()==1){
if(MaxLength<currLength){
MaxLength = currLength;
cur = i;
}
currLength = 0;
lastLetter = -1;
continue;
}
int front = letterFront(str);
int back = letterBack(str);
if (lastLetter == -1 || lastLetter == front) {
currLength++;
if(MaxLength<currLength){
MaxLength = currLength;
cur = i;
}
lastLetter = back;
} else {
if(MaxLength<currLength){
MaxLength = currLength;
cur = i;
}
currLength = 1;
lastLetter = back;
}
// if (MaxLength == 5) {
// System.out.println(str);
// System.out.println(i);
// }
}
System.out.println("最大链是"+MaxLength);
String s= "";
for(int i=cur-MaxLength+1;i<=cur;i++){
if(Objects.equals(others[i], "")||Objects.equals(others[i]," "))continue;
s+=others[i];
s+=" ";
}
System.out.println(s);
File writeName = new File("D:\\IDEA项目\\output1.txt");
if (!writeName.exists()){
writeName.createNewFile();
}
FileWriter writer = new FileWriter(writeName);
BufferedWriter out = new BufferedWriter(writer);
out.write(String.valueOf(MaxLength)+"\n");
out.write("链是:"+s);
out.flush(); // 把缓存区内容压入文件
out.close();
}
}