加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

ping监控窗口的java实现

(2012-04-26 08:21:25)
标签:

杂谈

04-13开始尝试写,初始代码仅实现指定IP是否可到达.重写框架,使用系统自带ping命令
04-16初步实现图形界面,图形界面布局采用固定格式;信息输出为向上滚动。
04-18信息输出修改为黑底白字.
04-20修改布局,使用GridLayout布局,解决拖放窗口组件不能自动缩放的问题.
04-22添加声音播放类Sounds。实现声音报警,丢包阈值为3,即连续丢包三次声音报警.
04-23实现信息输出JTextArea光标自动跟随,信息输出向下滚动。
04-25修改丢包阈值为5,将显示IP的JTextField组件修改为JLabel;添加日志记录,每天每个信息输出对应一个log文件。

PS:不是纯java实现,调用了系统的ping命令。
PingFrame.java为界面,调用系统ping命令。
Sounds.java实现声音播放。
JPingFile.java对文件进行处理,主要用于写日志。


PingFrame.java源码

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.lang.Thread;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.awt.event.*;
import java.lang.Thread;
import java.net.*;
import java.nio.channels.*;
import java.util.*;
import java.util.regex.*;
import javax.sound.sampled.*;


class PingFrame  extends Thread
{
static String IP;static JTextArea TA;static String IDC;static boolean flag=true;static Calendar c=new GregorianCalendar();

public static SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static JFrame JF=new JFrame("Ping test By 郭聪");
public static JTextArea TA1=new JTextArea("TA1");
public static JScrollPane SP1=new JScrollPane(TA1);
public static JLabel TF1=new JLabel("");
public static JButton JB1=new JButton("开始1");
public static JLabel IDC1=new JLabel();
//public static JButton JB11=new JButton("结束1");

public static JTextArea TA2=new JTextArea("TA2");
public static JScrollPane SP2=new JScrollPane(TA2);
public static JLabel TF2=new JLabel("");
public static JButton JB2=new JButton("开始2");
public static JLabel IDC2=new JLabel();
//public static JButton JB21=new JButton("结束2");

public static JTextArea TA3=new JTextArea("TA3");
public static JScrollPane SP3=new JScrollPane(TA3);
public static JLabel TF3=new JLabel("");
public static JButton JB3=new JButton("开始3");
public static JLabel IDC3=new JLabel();
//public static JButton JB31=new JButton("结束3");

public static JTextArea TA4=new JTextArea("TA4");
public static JScrollPane SP4=new JScrollPane(TA4);
public static JLabel TF4=new JLabel("");
public static JButton JB4=new JButton("开始4");
public static JLabel IDC4=new JLabel();
//public static JButton JB41=new JButton("结束4");

public static JTextArea TA5=new JTextArea("TA5");
public static JScrollPane SP5=new JScrollPane(TA5);
public static JLabel TF5=new JLabel("");
public static JButton JB5=new JButton("开始5");
public static JLabel IDC5=new JLabel();
//public static JButton JB51=new JButton("结束5");

public static JTextArea TA6=new JTextArea("TA6");
public static JScrollPane SP6=new JScrollPane(TA6);
public static JLabel TF6=new JLabel("");
public static JButton JB6=new JButton("开始6");
public static JLabel IDC6=new JLabel();
//public static JButton JB61=new JButton("结束6");

public static JTextArea TA7=new JTextArea("TA7");
public static JScrollPane SP7=new JScrollPane(TA7);
public static JLabel TF7=new JLabel("");
public static JButton JB7=new JButton("开始7");
public static JLabel IDC7=new JLabel();
//public static JButton JB71=new JButton("结束7");

public static JTextArea TA8=new JTextArea("TA8");
public static JScrollPane SP8=new JScrollPane(TA8);
public static JLabel TF8=new JLabel("");
public static JButton JB8=new JButton("开始8");
public static JLabel IDC8=new JLabel();
//public static JButton JB81=new JButton("结束8");

public static JTextArea TA9=new JTextArea("TA9");
public static JScrollPane SP9=new JScrollPane(TA9);
public static JLabel TF9=new JLabel("");
public static JButton JB9=new JButton("开始9");
public static JLabel IDC9=new JLabel();
//public static JButton JB91=new JButton("结束9");

public static JTextArea TA0=new JTextArea("TA0");
public static JScrollPane SP0=new JScrollPane(TA0);
public static JLabel TF0=new JLabel("");
public static JButton JB0=new JButton("开始0");
public static JLabel IDC0=new JLabel();
//public static JButton JB01=new JButton("结束0");

public static JPanel JP1=new JPanel();
public static JPanel JP2=new JPanel();
public static JPanel JP3=new JPanel();
public static JPanel JP4=new JPanel();
public static JPanel JP5=new JPanel();
public static JPanel JP6=new JPanel();
public static JPanel JP7=new JPanel();
public static JPanel JP8=new JPanel();
public static JPanel JP9=new JPanel();
public static JPanel JP0=new JPanel();
PingFrame(String IP,JTextArea TA,String IDC)
{
this.IP=IP;
this.TA=TA;
this.IDC=IDC;
}
public static void main(String args[])
{
readIni();
//退出操作
JF.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);// 
JF.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){
if (JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog((Frame)e.getSource(),"真的要退出?"))
{
try
{
Runtime.getRuntime().exec("taskkill /im ping.exe /f");
}
catch (Exception ex)
{}
System.exit(0) ;
    }else 
    return ;}} );
//排版参数
int IFx=5;int IFy=0;int IFl=70;int IFw=30;//显示IDC标签
int TFx=100;int TFy=0;int TFl=100;int TFw=30;//显示IP输入框标签
int JBx=435;int JBy=0;int JBl=70;int JBw=30;//显示按钮
int SPx=5;int SPy=35;int SPl=500;int SPw=150;//显示输出消息
JF.setSize(1050,800);


//主窗体布局管理器
Container con=JF.getContentPane();
GridLayout grid =new GridLayout(5,2);
con.setLayout(grid);
//单个panel布局管理。所有控件放入一个JPanel中。再将JPanel放入JFrame中
JP1.setLayout(null);
JP1.add(TF1);JP1.add(JB1);JP1.add(SP1);JP1.add(IDC1);
IDC1.setVisible(true);IDC1.setBounds(IFx,IFy,IFl,IFw);
TF1.setVisible(true);TF1.setBounds(TFx,TFy,TFl,TFw);
TF1.setVisible(true);
JB1.setVisible(true);JB1.setBounds(JBx,JBy,JBl,JBw);
JB1.setVisible(true);
SP1.setVisible(true);SP1.setBounds(SPx,SPy,SPl,SPw);
TA1.setLineWrap(true);TA1.setVisible(true);TA1.setBackground(Color.black);TA1.setForeground(Color.WHITE);

JP2.setLayout(null);
JP2.add(TF2);JP2.add(JB2);JP2.add(SP2);JP2.add(IDC2);
IDC2.setVisible(true);IDC2.setBounds(IFx,IFy,IFl,IFw);
TF2.setVisible(true);TF2.setBounds(TFx,TFy,TFl,TFw);
TF2.setVisible(true);
JB2.setVisible(true);JB2.setBounds(JBx,JBy,JBl,JBw);
JB2.setVisible(true);
SP2.setVisible(true);SP2.setBounds(SPx,SPy,SPl,SPw);
TA2.setLineWrap(true);TA2.setVisible(true);TA2.setBackground(Color.black);TA2.setForeground(Color.WHITE);

JP3.setLayout(null);
JP3.add(TF3);JP3.add(JB3);JP3.add(SP3);JP3.add(IDC3);
IDC3.setVisible(true);IDC3.setBounds(IFx,IFy,IFl,IFw);
TF3.setVisible(true);TF3.setBounds(TFx,TFy,TFl,TFw);
TF3.setVisible(true);
JB3.setVisible(true);JB3.setBounds(JBx,JBy,JBl,JBw);
JB3.setVisible(true);
SP3.setVisible(true);SP3.setBounds(SPx,SPy,SPl,SPw);
TA3.setLineWrap(true);TA3.setVisible(true);TA3.setBackground(Color.black);TA3.setForeground(Color.WHITE);

JP4.setLayout(null);
JP4.add(TF4);JP4.add(JB4);JP4.add(SP4);JP4.add(IDC4);
IDC4.setVisible(true);IDC4.setBounds(IFx,IFy,IFl,IFw);
TF4.setVisible(true);TF4.setBounds(TFx,TFy,TFl,TFw);
TF4.setVisible(true);
JB4.setVisible(true);JB4.setBounds(JBx,JBy,JBl,JBw);
JB4.setVisible(true);
SP4.setVisible(true);SP4.setBounds(SPx,SPy,SPl,SPw);
TA4.setLineWrap(true);TA4.setVisible(true);TA4.setBackground(Color.black);TA4.setForeground(Color.WHITE);

JP5.setLayout(null);
JP5.add(TF5);JP5.add(JB5);JP5.add(SP5);JP5.add(IDC5);
IDC5.setVisible(true);IDC5.setBounds(IFx,IFy,IFl,IFw);
TF5.setVisible(true);TF5.setBounds(TFx,TFy,TFl,TFw);
TF5.setVisible(true);
JB5.setVisible(true);JB5.setBounds(JBx,JBy,JBl,JBw);
JB5.setVisible(true);
SP5.setVisible(true);SP5.setBounds(SPx,SPy,SPl,SPw);
TA5.setLineWrap(true);TA5.setVisible(true);TA5.setBackground(Color.black);TA5.setForeground(Color.WHITE);

JP6.setLayout(null);
JP6.add(TF6);JP6.add(JB6);JP6.add(SP6);JP6.add(IDC6);
IDC6.setVisible(true);IDC6.setBounds(IFx,IFy,IFl,IFw);
TF6.setVisible(true);TF6.setBounds(TFx,TFy,TFl,TFw);
TF6.setVisible(true);
JB6.setVisible(true);JB6.setBounds(JBx,JBy,JBl,JBw);
JB6.setVisible(true);
SP6.setVisible(true);SP6.setBounds(SPx,SPy,SPl,SPw);
TA6.setLineWrap(true);TA6.setVisible(true);TA6.setBackground(Color.black);TA6.setForeground(Color.WHITE);

JP7.setLayout(null);
JP7.add(TF7);JP7.add(JB7);JP7.add(SP7);JP7.add(IDC7);
IDC7.setVisible(true);IDC7.setBounds(IFx,IFy,IFl,IFw);
TF7.setVisible(true);TF7.setBounds(TFx,TFy,TFl,TFw);
TF7.setVisible(true);
JB7.setVisible(true);JB7.setBounds(JBx,JBy,JBl,JBw);
JB7.setVisible(true);
SP7.setVisible(true);SP7.setBounds(SPx,SPy,SPl,SPw);
TA7.setLineWrap(true);TA7.setVisible(true);TA7.setBackground(Color.black);TA7.setForeground(Color.WHITE);
JP8.setLayout(null);
JP8.add(TF8);JP8.add(JB8);JP8.add(SP8);JP8.add(IDC8);
IDC8.setVisible(true);IDC8.setBounds(IFx,IFy,IFl,IFw);
TF8.setVisible(true);TF8.setBounds(TFx,TFy,TFl,TFw);
TF8.setVisible(true);
JB8.setVisible(true);JB8.setBounds(JBx,JBy,JBl,JBw);
JB8.setVisible(true);
SP8.setVisible(true);SP8.setBounds(SPx,SPy,SPl,SPw);
TA8.setLineWrap(true);TA8.setVisible(true);TA8.setBackground(Color.black);TA8.setForeground(Color.WHITE);


JP9.setLayout(null);
JP9.add(TF9);JP9.add(JB9);JP9.add(SP9);JP9.add(IDC9);
IDC9.setVisible(true);IDC9.setBounds(IFx,IFy,IFl,IFw);
TF9.setVisible(true);TF9.setBounds(TFx,TFy,TFl,TFw);
TF9.setVisible(true);
JB9.setVisible(true);JB9.setBounds(JBx,JBy,JBl,JBw);
JB9.setVisible(true);
SP9.setVisible(true);SP9.setBounds(SPx,SPy,SPl,SPw);
TA9.setLineWrap(true);TA9.setVisible(true);TA9.setBackground(Color.black);TA9.setForeground(Color.WHITE);

JP0.setLayout(null);
JP0.add(TF0);JP0.add(JB0);JP0.add(SP0);JP0.add(IDC0);
IDC0.setVisible(true);IDC0.setBounds(IFx,IFy,IFl,IFw);
TF0.setVisible(true);TF0.setBounds(TFx,TFy,TFl,TFw);
TF0.setVisible(true);
JB0.setVisible(true);JB0.setBounds(JBx,JBy,JBl,JBw);
JB0.setVisible(true);
SP0.setVisible(true);SP0.setBounds(SPx,SPy,SPl,SPw);
TA0.setLineWrap(true);TA0.setVisible(true);TA0.setBackground(Color.black);TA0.setForeground(Color.WHITE);

con.add(JP1);con.add(JP2);con.add(JP3);con.add(JP4);con.add(JP5);
con.add(JP6);con.add(JP7);con.add(JP8);con.add(JP9);con.add(JP0);

JB1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF1.getText(),TA1,IDC1.getText()).start();JB1.setEnabled(false);}});//JB21.setEnabled(true);
JB2.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF2.getText(),TA2,IDC2.getText()).start();JB2.setEnabled(false);}});
JB3.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF3.getText(),TA3,IDC3.getText()).start();JB3.setEnabled(false);}});
JB4.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF4.getText(),TA4,IDC4.getText()).start();JB4.setEnabled(false);}});
JB5.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF5.getText(),TA5,IDC5.getText()).start();JB5.setEnabled(false);}});
JB6.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF6.getText(),TA6,IDC6.getText()).start();JB6.setEnabled(false);}});
JB7.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF7.getText(),TA7,IDC7.getText()).start();JB7.setEnabled(false);}});
JB8.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF8.getText(),TA8,IDC8.getText()).start();JB8.setEnabled(false);}});
JB9.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF9.getText(),TA9,IDC9.getText()).start();JB9.setEnabled(false);}});
JB0.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e)
{new  PingFrame(TF0.getText(),TA0,IDC0.getText()).start();JB0.setEnabled(false);}});

JF.setVisible(true);
}

static void  ping(String IP,JTextArea TA,String IDC)
{
String line = null;
String command = "ping -t "+IP;
try
{
//Sounds S =new Sounds("UNIT.WAV");
while (flag)
{
TA.setRows(20);
Process pro = Runtime.getRuntime().exec(command);
BufferedReader buf = new BufferedReader(new InputStreamReader(pro.getInputStream()));
int i=0;
while ((line = buf.readLine()) != null && flag)
{

if(line.equals(""))
{}
else if(line.equals("Request timed out."))
{
TA.append("\n"+DF.format(new Date()) + " " + line );
TA.setSelectionStart(TA.getText().length());
System.out.println(DF.format(new Date()) +" "+ line);
i++;
insertlog(IDC,DF.format(new Date()) + " " + IP + " " + line + "\r\n");//写日志操作
if(i>=5)
{

new Sounds("UNIT.WAV").play();
System.out.println("报警ing");
i=5;//如果连续报警,保持报警阈值为5
}
}
else 
{
TA.append("\n"+DF.format(new Date()) + " " + line );
TA.setSelectionStart(TA.getText().length());
//System.out.println(DF.format(new Date()) +" "+ line+TA.getText().length());
if(i>0)
i--;//报警阈值减小
else 
i=0;//报警阈值归0
}
//System.out.println(i+line+flag);
//if(TA.getLineCount()>300){TA.setText("");}
if(TA.getText().length()>3000 ) {TA.replaceRange("", 0, TA.getText().length() - 2000);}
}
}
}
catch (Exception ex){System.out.println(ex.getMessage());}
}
public void run()
{
ping (IP,TA,IDC);
}

public static void readIni(){
Properties p = new Properties();
try {
InputStream inputStream = new BufferedInputStream (new FileInputStream("ip.txt"));
p.load(inputStream);
}
catch (IOException e1) {
e1.printStackTrace();
}
if (p.getProperty("IP1")!=null)
TF1.setText(p.getProperty("IP1"));
if (p.getProperty("IP2")!=null)
TF2.setText(p.getProperty("IP2"));
if (p.getProperty("IP3")!=null)
TF3.setText(p.getProperty("IP3"));
if (p.getProperty("IP4")!=null)
TF4.setText(p.getProperty("IP4"));
if (p.getProperty("IP5")!=null)
TF5.setText(p.getProperty("IP5"));
if (p.getProperty("IP6")!=null)
TF6.setText(p.getProperty("IP6"));
if (p.getProperty("IP7")!=null)
TF7.setText(p.getProperty("IP7"));
if (p.getProperty("IP8")!=null)
TF8.setText(p.getProperty("IP8"));
if (p.getProperty("IP9")!=null)
TF9.setText(p.getProperty("IP9"));
if (p.getProperty("IP0")!=null)
TF0.setText(p.getProperty("IP0"));
if (p.getProperty("IDC1")!=null)
IDC1.setText(p.getProperty("IDC1"));
if (p.getProperty("IDC2")!=null)
IDC2.setText(p.getProperty("IDC2"));
if (p.getProperty("IDC3")!=null)
IDC3.setText(p.getProperty("IDC3"));
if (p.getProperty("IDC4")!=null)
IDC4.setText(p.getProperty("IDC4"));
if (p.getProperty("IDC5")!=null)
IDC5.setText(p.getProperty("IDC5"));
if (p.getProperty("IDC6")!=null)
IDC6.setText(p.getProperty("IDC6"));
if (p.getProperty("IDC7")!=null)
IDC7.setText(p.getProperty("IDC7"));
if (p.getProperty("IDC8")!=null)
IDC8.setText(p.getProperty("IDC8"));
if (p.getProperty("IDC9")!=null)
IDC9.setText(p.getProperty("IDC9"));
if (p.getProperty("IDC0")!=null)
IDC0.setText(p.getProperty("IDC0"));
}
public static void insertlog(String IDC,String log){
String fileName=nowData()+"_"+IDC + ".log";
File file = new File(fileName);
if (!file.exists()) {
JPingFile.CreatFile(fileName);
}
JPingFile.appendMethodB(fileName,log);

}
public static String nowData(){
int year=c.get(c.YEAR);
int month=c.get(c.MONTH)+1;
int day=c.get(c.DAY_OF_MONTH);
return (year+"-"+month+"-"+day);
}
}

Sounds.java源码

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.*;


public class Sounds { 

private AudioFormat audioFormat = null; 
private SourceDataLine sourceDataLine = null; 
private DataLine.Info dataLine_info = null; 
private String file = "./intel.wav"; 
private AudioInputStream audioInputStream = null; 



public Sounds(String file) throws LineUnavailableException, UnsupportedAudioFileException, IOException{ 

audioInputStream = AudioSystem.getAudioInputStream(new File(file)); 
audioFormat = audioInputStream.getFormat(); 
dataLine_info = new DataLine.Info(SourceDataLine.class,audioFormat); 
sourceDataLine = (SourceDataLine)AudioSystem.getLine(dataLine_info); 

public void play() throws IOException, LineUnavailableException

byte[] b = new byte[1024]; 
int len = 0; 
sourceDataLine.open(audioFormat, 1024); 
sourceDataLine.start(); 
while ((len = audioInputStream.read(b)) > 0){ 
sourceDataLine.write(b, 0, len); 
audioInputStream.close(); 
sourceDataLine.drain(); 
sourceDataLine.close(); 


JPingFile.java源码(网上抄的只用了几个方法)
import java.io.*;


class JPingFile 
{

public static void CreatFile(String fileName){
File file = new File(fileName);
if (!file.exists()) { //判断文件是否存在
try {
file.createNewFile(); //创建文件
System.out.println("文件创建成功!");
}
catch (IOException e) {
e.printStackTrace();
}
}
else
System.out.println("文件已存在 ,创建失败!");
}
public static void DeleteFile(String fileName){
File file = new File(fileName);
if (file.exists()) {                     // 判断文件是否存在
file.delete(); //删除文件
System.out.println("删除文件成功!");
}else
System.out.println("文件不存在,删除文件失败");
}

 
public static void readFileByBytes(String fileName)
{  
File file = new File(fileName);  
InputStream in = null;  
try {  
System.out.println("以字节为单位读取文件内容,一次读一个字节:");  
// 一次读一个字节  
in = new FileInputStream(file);  
int tempbyte;  
while((tempbyte=in.read()) != -1){  
System.out.write(tempbyte);  
}  
in.close();  
}
catch (IOException e) {  
e.printStackTrace();  
return;  
}
try {  
System.out.println("以字节为单位读取文件内容,一次读多个字节:");  
//一次读多个字节  
byte[] tempbytes = new byte[100];  
int byteread = 0;  
in = new FileInputStream(fileName);  
//ReadFromFile.showAvailableBytes(in);  
//读入多个字节到字节数组中,byteread为一次读入的字节数  
while ((byteread = in.read(tempbytes)) != -1){  
System.out.write(tempbytes, 0, byteread);  
}  
}
catch (Exception e1) {  
e1.printStackTrace();  
}
finally{
if (in != null){  
try {  
in.close();  
}
catch (IOException e1) {}  
}
}
}
 
public static void readFileByChars(String fileName){  
File file = new File(fileName);  
Reader reader = null;  
try {
System.out.println("以字符为单位读取文件内容,一次读一个字节:");  
// 一次读一个字符  
reader = new InputStreamReader(new FileInputStream(file));  
int tempchar;  
while ((tempchar = reader.read()) != -1){  
//对于windows下,rn这两个字符在一起时,表示一个换行。  
//但如果这两个字符分开显示时,会换两次行。  
//因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。  
if (((char)tempchar) != 'r'){
System.out.print((char)tempchar); 
}
}  
reader.close();  
}
catch (Exception e) {
e.printStackTrace();
}  
try {  
System.out.println("以字符为单位读取文件内容,一次读多个字节:");  
//一次读多个字符  
char[] tempchars = new char[30];  
int charread = 0;  
reader = new InputStreamReader(new FileInputStream(fileName));  
//读入多个字符到字符数组中,charread为一次读取字符数  
while ((charread = reader.read(tempchars))!=-1){  
//同样屏蔽掉r不显示  
if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] != 'r')){  
System.out.print(tempchars);  
}
else{  
for (int i=0; i<charread; i++){  
if(tempchars[i] == 'r'){  
continue;
}
else{
System.out.print(tempchars[i]);  
}  
}  
}  
}  
}
catch (Exception e1) {  
e1.printStackTrace();  
}
finally {  
if (reader != null){  
try {  
reader.close();  
}
catch (IOException e1) {  
}  
}  
}
}


 
public static void appendMethodA(String fileName,String content){  
try {  
// 打开一个随机访问文件流,按读写方式  
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");  
// 文件长度,字节数  
long fileLength = randomFile.length();  
//将写文件指针移到文件尾。  
randomFile.seek(fileLength);  
randomFile.writeBytes(content);  
randomFile.close();  
}
catch (IOException e){  
e.printStackTrace();  
}  
}  



 
public static void appendMethodB(String fileName, String content){  
try {  
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件  
FileWriter writer = new FileWriter(fileName, true);  
writer.write(content);  
writer.close();  
}
catch (IOException e) {  
e.printStackTrace();  
}  
}  

public static void main(String[] args) {  
String fileName = "newTemp.txt";  
CreatFile(fileName);
String content = "new append!";  
//按方法A追加文件  
appendMethodB(fileName, content);  
appendMethodB(fileName, "append end. n");  
//显示文件内容  
//ReadFromFile.readFileByLines(fileName);  
//按方法B追加文件  
//AppendToFile.appendMethodB(fileName, content);  
//AppendToFile.appendMethodB(fileName, "append end. n");  
//显示文件内容  
//readFileByLines(fileName);  
}   

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有