📄 vcardsplitter.java
字号:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
* <p>
* A utility to split concatenated VCard (IETF RFC 2426) format inputFile into
* separate files. It simply splits based on the "BEGIN:VCARD" and "END:VCARD"
* tags.
* </p>
* <p>
* This utility was created to help with the import of a Lotus Organizer 6
* .vcf format export inputFile (which concatenates all record) into Palm Desktop
* (which expects only a single record per inputFile). It may be useful for other
* purposes.
* </p>
* <p>
* This is a very simple class, with minimal error handling. Feel free to
* improve it, it has accomplished the purpose I wanted :)
*
* @author Chad Woolley
*
*/
public class VCardSplitter {
/**
* the input inputFile
* */
private File inputFile;
/**total number of lines*/
private int numberOfLines = 0;
/**total number of words*/
private int numberOfWords = 0;
/**total number of characters in lines*/
private int totalLineLengths = 0;
/**total number of characters in words*/
private int totalWordLengths = 0;
/**
* Allow user to select the desire input inputFile
*/
public void selectFile() {
StringBuffer instructions = new StringBuffer();
instructions.append("This program will split multiple concatenated vCard files\n");
instructions.append("into separate files. All concatenated vCard records contained\n");
instructions.append("within the input file will be written to separate output\n");
instructions.append("files in the same directory. The output files will be named\n");
instructions.append("'<input filename>.rec-<n>.vcf', where <input filename> is the name\n");
instructions.append("of the input file (including extension), and <n> is the record number. \n");
instructions.append("See http://sourceforge.net/projects/vcardsplitter for more info. \n");
instructions.append("Press OK to select the file to split. \n");
JFileChooser chooser = new JFileChooser();
JOptionPane.showMessageDialog(chooser, instructions.toString());
chooser.setDialogTitle("Select the file to split:");
chooser.showOpenDialog(null);
inputFile = chooser.getSelectedFile();
}
/**
* read in the inputFile, split it into records, and write out to
* separate files.
**/
public void read() throws FileNotFoundException, IOException {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
int recordCount = 1;
ArrayList record = new ArrayList();
while (reader.ready()) {
String inputFileLine = reader.readLine();
record.add(inputFileLine);
if (inputFileLine.equalsIgnoreCase("END:VCARD")) {
writeOutputFile(record, recordCount);
recordCount++;
record.clear();
}
}
}
private void writeOutputFile(ArrayList record, int recordCount) {
System.out.println("==================");
Iterator iterator = record.iterator();
FileOutputStream out = null;
String outputfileName =
inputFile.getAbsolutePath() + ".rec-" + recordCount + ".vcf";
try {
out = new FileOutputStream(outputfileName);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
PrintStream printStream = new PrintStream(out);
while (iterator.hasNext()) {
String recordLine = (String) iterator.next();
try {
printStream.println(recordLine);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
System.out.println(recordLine);
}
printStream.close();
}
/**
* Main method
*/
public static void main(String[] argv)
throws FileNotFoundException, IOException {
VCardSplitter vCardSplitter = new VCardSplitter();
vCardSplitter.selectFile();
if (vCardSplitter.inputFile == null) {
System.exit(1);
}
vCardSplitter.read();
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -