📄 wjcaozuo.java
字号:
/*
*
* 文件操作处理
*
* 2007/08/22
*
*/
package serial;
import java.io.*;
import javax.swing.*;
public class WJCaoZuo implements Runnable
{
int ZiJie; //记录文件字节位数
Thread athread;
File file = null;
byte buffer[]=new byte[1024];
FileInputStream in = null;
FileOutputStream out=null,out1=null;
ProgressMonitor pMonitor = null;
JEditorPane jtext = new JEditorPane();
ProgressMonitorInputStream pmInputStream = null;
//文件复制函数
public void copyFile(String src,String dest,String dest6)
{
file = new File(dest);
if(!file.exists())
{
Copy(src,dest,dest6); //调用复制函数
}
else
{
ZiJie = JOptionPane.showConfirmDialog(null," 文件名已存在,是否覆盖?","系统提示",JOptionPane.YES_NO_OPTION);
if(ZiJie==JOptionPane.YES_OPTION)
{
Copy(src,dest,dest6);
}
}
}
//文件写入
public void ReadFile(String src1,String dest1)
{
file = new File(dest1);
if(file!=null)
{
try
{
out1 = new FileOutputStream(file);
}
catch(FileNotFoundException fe){}
try
{
out1.write(src1.getBytes());
}
catch(IOException ioe){}
finally
{
try
{
if(out1 != null)
out1.close();
} catch(IOException ioe2){}
}
}
}
//文件读出
public String WriteFile(String dest2)
{
file = new File(dest2);
if(file.isFile())
{
try
{
jtext.setPage("file:"+dest2);
}
catch(IOException ioe)
{
ioe.printStackTrace(System.err);
}
}
return jtext.getText();
}
//删除文件
public void DeleteFile(String dest3)
{
file = new File(dest3);
if(file.isFile())
{
file.delete();
}
}
//复制函数
public void Copy(String src2,String dest4,String dest5)
{
file = new File(dest4);
try
{
file.createNewFile();
in = new FileInputStream(src2);
out = new FileOutputStream(file);
pmInputStream = new ProgressMonitorInputStream(null,dest5,in);
pMonitor = pmInputStream.getProgressMonitor();
pMonitor.setMillisToDecideToPopup(10);
pMonitor.setMillisToPopup(0);
athread = new Thread(this);
athread.start();
}
catch(IOException e){}
}
//读取文件线程
public void run()
{
try
{
while((ZiJie=pmInputStream.read(buffer))!=-1)
{
for(int i=0;i<ZiJie;i++)
{
out.write(buffer[i]);
}
if(pMonitor.isCanceled())
{
pmInputStream.close();
in.close();
out.close();
athread.stop();
}
}
JOptionPane.showMessageDialog(null," 数据处理完成!","系统提示",JOptionPane.INFORMATION_MESSAGE);
pmInputStream.close();
in.close();
out.close();
out = null;
athread.stop();
}
catch(IOException e){}
}
public void renameFile(String path,String oldname,String newname)
{
if(!oldname.equals(newname))
{//新的文件名和以前文件名不同时,才有必要进行重命名
File oldfile=new File(path+"/"+oldname);
File newfile=new File(path+"/"+newname);
if(newfile.exists())
{//若在该目录下已经有一个文件和新文件名相同,则不允许重命名
newfile=new File(path+"/@"+newname);
}
else
{
oldfile.renameTo(newfile);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -