package exercise_9;
import java.util.*;
import java.io.*;
public class Exercise9_16 {
/**Main method*/
public static void main(String[] args) throws Exception {
// Check command line parameter usage
if (args.length != 1) {
System.out.println(
"Usage: java Exercise9_16 filename");
System.exit(0);
}
// Check if source file exists
File sourceFile = new File(args[0]);
if (!sourceFile.exists()) {
System.out.println("Source file " + args[0] + " not exist");
System.exit(0);
}
StringBuilder buffer = new StringBuilder();
Scanner input = new Scanner(sourceFile);
while (input.hasNext()) {
String s = input.nextLine();//一行行输入
String s1 = s.trim();//把多出来的空格都删掉
System.out.println(s1);
if (s1.charAt(0) == '{') {
buffer.append(" {");
if (s1.length() > 1) buffer.append("\r\n" + s.replace('{', ' '));
}
else
buffer.append("\r\n" + s);
}
input.close();
// Write buffer into the file
PrintWriter output = new PrintWriter(sourceFile);
output.print(buffer.toString());
output.close();
}
}
package exercise_9;
import java.util.*;
import java.io.*;
public class Exercise9_16 {
/**Main method*/
public static void main(String[] args) throws Exception {
// Check command line parameter usage
if (args.length != 1) //判断输入的参数是否是一个, 如果不是,则打印下面信息
System.out.println(
"Usage: java Exercise9_16 filename");
System.exit(0); //然后退出程序
}
// Check if source file exists
File sourceFile = new File(args[0]); //创建一个以参数名为文件名的文件
if (!sourceFile.exists()) { // 判断文件是否存在 ,如果不存在 则打印一下信息 ,退出程序
System.out.println("Source file " + args[0] + " not exist");
System.exit(0);
}
StringBuilder buffer = new StringBuilder(); // 创建一个字符串容器
Scanner input = new Scanner(sourceFile); //扫描文件
while (input.hasNext()) { //判断文件内容是否存在,
String s = input.nextLine();//一行行输入 //把文件内容读入 s 字符串中
String s1 = s.trim();//把多出来的空格都删掉 //把s字符串中的空格去掉
System.out.println(s1); // 打印s1
if (s1.charAt(0) == '{') { // 判断字符串s1中第一个字符是否为‘{’
buffer.append(" {"); // 如果是 ,则向字符串容器中添加 ‘{’
if (s1.length() > 1) buffer.append("\r\n" + s.replace('{', ' ')); // 如果s1的长度大于1, 则向字符串容器中添加换行及下行首位置,把s里‘ 用‘{换
}
else
buffer.append("\r\n" + s); // 否则buffer字符串容器添加换行 及首位置, s字符串
}
// Write buffer into the file
PrintWriter output = new PrintWriter(sourceFile); // 输出流 把先前的文件 连接一条管道 即输出流
output.print(buffer.toString()); // 输出buffer容器里的字符
output.close();
}
}
希望对你有帮助 ,谢谢
关于PrintWriter你可以百度一下
public class Exercise9_16 {
/**Main method*/
public static void main(String[] args) throws Exception {
// Check command line parameter usage
if (args.length != 1) {//需要你输入一个参数,也就是args,如果参数不止一个执行下面代码
System.out.println(
"Usage: java Exercise9_16 filename");
System.exit(0);
}
// Check if source file exists//
File sourceFile = new File(args[0]);//以参数作为文件的名字
if (!sourceFile.exists()) {//如果以参数为文件名的文件不存在,就执行下面代码
System.out.println("Source file " + args[0] + " not exist");
System.exit(0);
}
StringBuilder buffer = new StringBuilder();
Scanner input = new Scanner(sourceFile);//扫描文件
while (input.hasNext()) {//当文件存在内容时
String s = input.nextLine();//一行行输入
String s1 = s.trim();//把多出来的空格都删掉
System.out.println(s1);
if (s1.charAt(0) == '{') {
buffer.append(" {");
if (s1.length() > 1) buffer.append("\r\n" + s.replace('{', ' '));
}
else
buffer.append("\r\n" + s);
}
input.close();
// Write buffer into the file
PrintWriter output = new PrintWriter(sourceFile);//将buffer内容写到以参数命名的文件里
output.print(buffer.toString());//将buffer内容写到以参数命名的文件里
output.close();
}
}
如果回答对您有帮助,请采纳
还有Scanner也可以百度
if (s1.charAt(0) == '{') {//如果读取的第一行代码的第一个字符是“{”
buffer.append(" {");//在buffer中添加"{"
if (s1.length() > 1) buffer.append("\r\n" + s.replace('{', ' '));//如果第一行字符大于1,buffer就换行,然后就用空字符替换"{"
}
else
buffer.append("\r\n" + s);//如果第一个字符不是"{",就换行,然后输出该行字符
}
input.close();
我仔细看了这段,好像没有换成"}"这个字符的
如果回答对您有帮助,请采纳
这个程序就是一个文件浏览程序,把指定文件的信息输出到控制台。
首先运行这个程序需要传入一个表示文件名的参数;然后读取文件的内容并打印到控制台。