📄 warwriter.java
字号:
package library;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class WarWriter {
protected String xml = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>"
+ "<custinfo>" + "<account>75788228701</account>"
+ "<yewu>ZDDHFF1023360</yewu>" + "<cert>440624493113441</cert>"
+ "<name>黄日安</name>" + "<tel>88228701</tel>" + "</custinfo>";
@SuppressWarnings("unused")
private String directoryName=null;
public static final int DIRECTORY=423;
public static final int FILE=23;
public WarWriter(String directoryName) throws IOException {
this.directoryName=directoryName;
check(directoryName, DIRECTORY);
System.out.println(directoryName);
}
public boolean write(String string,String fileName) throws JDOMException, IOException
{
String path=mergeName(fileName);
System.out.println(path);
check(path,FILE);
FileOutputStream fs = new FileOutputStream(new File(path).getAbsolutePath());
Document xmldoc = new SAXBuilder().build(new StringReader(string));
new XMLOutputter().output(xmldoc, fs);
fs.close();
return true;
}
public boolean write(Document document,String fileName) throws IOException
{
String path=mergeName(fileName);
System.out.println(path);
check(path,FILE);
FileOutputStream fs = new FileOutputStream(path);
new XMLOutputter().output(document, fs);
fs.close();
return true;
}
public boolean writeDefault(String fileName) throws JDOMException, IOException
{
return write(xml,fileName);
}
private String mergeName(String fileName)
{
char c=directoryName.charAt(directoryName.length()-1);
if(c=='\\'||c=='/')
{
return directoryName+fileName;
}
else
{
return directoryName+"\\"+fileName;
}
}
public void check(String path,int type) throws IOException
{
File file=new File(path);
if(file.exists()==false)
{
file.createNewFile();
System.out.println("create");
}
else
{
if(type==FILE&&file.isDirectory())
{
throw new IOException("parameter error : not a file");
}
else
{
if(type==DIRECTORY&&file.isFile())
{
throw new IOException("parameter error : not a directory");
}
else
{
System.out.print((file.isFile()?"file ":"directory "));
System.out.println("exist");
}
}
}
}
/*public void checkFile(String fileName) throws IOException
{
File file=new File(fileName);
if(file.exists()==false)
{
file.createNewFile();
System.out.println("create");
}
else
{
if(file.isDirectory())
{
throw new IOException("the parameter of fileName is a directory");
}
else
{
System.out.println("exist");
}
}
}
public void checkDirectory(String directoryName) throws IOException
{
File file=new File(directoryName);
if(file.exists()==false)
{
file.createNewFile();
System.out.println("create");
}
else
{
if(file.isDirectory())
{
System.out.println("exist");
}
else
{
throw new IOException("the parameter of directoryName is not a directory");
}
}
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -