日期:2014-05-20 浏览次数:21042 次
public class Test
{
public static void main(String[] args)
{
//Some statements
}
}
public class Test{
public static void main(String[] args){
//Some statements
}
}
package ex09;
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
public class ex09_16 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
JFileChooser fileChooser=new JFileChooser();
if(fileChooser.showOpenDialog(null)
==JFileChooser.APPROVE_OPTION){
File sourceFile=fileChooser.getSelectedFile();
Scanner input=new Scanner(sourceFile);
PrintWriter output=new PrintWriter(sourceFile);
while(input.hasNext()){
while(input.next().equals("{")){
output.println("\r{");
}
}
input.close();
output.close();
}
}
}
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
public class Main {
private static final String NONE = "NONE";//当读取的第二行没有时,设置为该字符串
public static void main(String[] args) throws Exception {
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File sourceFile = fileChooser.getSelectedFile();
Scanner input = new Scanner(sourceFile);
PrintWriter output = new PrintWriter(new File(sourceFile.getName()));
//每次都去读两行(如果第二行没有的话,就设置为"NONE")
String first = "";//保存读取到的第一行
String second = "";//保存读取到的第二行
input.useDelimiter("\r\n");//设置分割符为换行。默认情况下该分隔符模式与空白匹配
while (input.hasNext()) {
first = input.next();//读取第一行
if(input.hasNext()) {//如果第二行存在的话,读取第二行
second = input.next();
} else {//如果没有第二行,就设置为"NONE"
second = NONE;
}
//如果第二行为"{",就把第二行加到第一行后面写到文件中
if(second.trim().equals("{")) {
output.println(first + second);
} else {//否则的话,先输出第一行,如果第二行存在的话,接着输出第二行
output.println(first);
if(!second.equals(NONE)) {
output.println(second);
}
}
}
input.close();
output.close();
}
}
}