完整代码
“ABC 123 1 2 ABC‘123 1 2”2018年1月30日2:34:13:000 AM“DD1 1”2018年1月30日2:56:08:000 AM“EST‘ABC 20180821\r\n”;
system.out.println(“替换后开始输入-------------------------------------------------------------------------------”);
系统.out.println(trimm);
}
ABC 123 1 2 ABC'123 1 2 2018年1月30日2:34:13:000 AM“DD1”2018年1月30日2:56:08:000 AM“EST”ABC 20180821
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
public static void main(string[]args)throws exception{
“ABC 123 1 2 ABC‘123 1 2”2018年1月30日2:34:13:000 AM“DD1 1”2018年1月30日2:56:08:000 AM“EST‘ABC 20180821\r\n”;
fileoutputstream fos=新的fileoutputstream(fout);
bw.写入(trimm);
}catch(fileNotFoundException E){
}捕获(IOException E){
}
public static void main(string[]args)throws exception{
路径路径=path.get(“path”);
{
}
string trimm=“abc 123 1 2 abc”123 1 2“2018年1月30日2:34:13:000am\”dd1 1“2018年1月30日2:56:08:000am\”est“abc 20180821\n”+
files.write(paths.get(“path”),trimm.replacefirst(“[\n\r]+$”,“”).getbytes());
}
这三个密码都给了我:
完整代码
public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";
System.out.println("---------------------------------------------------Before replace Start of the input---------------------------------------------------");
System.out.println(trimm);
System.out.println("---------------------------------------------------Before replace End of the input---------------------------------------------------");
System.out.println("---------------------------------------------------After replace Start of the input---------------------------------------------------");
trimm = trimm.replaceFirst("[\n\r]+$", "");
System.out.println(trimm);
System.out.println("---------------------------------------------------After replace End of the input---------------------------------------------------");
}
输出:
---------------------------------------------------Before replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
---------------------------------------------------Before replace End of the input---------------------------------------------------
---------------------------------------------------After replace Start of the input---------------------------------------------------
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
ABC 123|1|2 ABC '123|1|2|"Jan 30 2018 2:34:13:000AM"|dd1|1|"Jan 30 2018 2:56:08:000AM"|EST' ABC 20180821
---------------------------------------------------After replace End of the input---------------------------------------------------
Ideone demo
我尝试了这三种解决方案:
public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";
try {
File fout = new File("path");
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
trimm = trimm.replaceAll("[\n\r]+$", "");
bw.write(trimm);
//bw.newLine();//<-----------------------note this
bw.close();
} catch (FileNotFoundException e) {
// File was not found
e.printStackTrace();
} catch (IOException e) {
// Problem when writing to the file
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";
Path path = Paths.get("path");
try (BufferedWriter writer = Files.newBufferedWriter(path))
{
writer.write(trimm.replaceFirst("[\n\r]+$", ""));
}
}
public static void main(String[] args) {
String trimm = "ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\n" +
"ABC 123|1|2 ABC '123|1|2|\"Jan 30 2018 2:34:13:000AM\"|dd1|1|\"Jan 30 2018 2:56:08:000AM\"|EST' ABC 20180821\r\n";
try {
Files.write(Paths.get("path"), trimm.replaceFirst("[\n\r]+$", "").getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
