Java Swing客户端小项目

时间:2021-11-24 21:23:56

记录一下两个用java swing写的客户端。

项目1:

关键词:swing  jtable

代码如下:

1、主类:

package com.my.agent.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException; import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter; public class AgentClinet implements ActionListener{ JTextField textField = null;
MyTabelModel tm = null;
JFrame jframe = null;
public AgentClinet(){
jframe = new JFrame("");
jframe.setSize(600, 400);
jframe.setBackground(Color.BLUE);
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setLayout(new FlowLayout()); //上面按钮
JPanel jpanel = new JPanel();
JButton button = new JButton("添加信息");
button.addActionListener(this);
jpanel.add(button);
button = new JButton("一键启动");
button.addActionListener(this);
jpanel.add(button);
button = new JButton("一键停止");
button.addActionListener(this);
jpanel.add(button);
jframe.add(jpanel); //数据展示
tm = new MyTabelModel(null);
final JTable table = new JTable(tm);
table.setPreferredScrollableViewportSize(new Dimension(550, 220));
JScrollPane s = new JScrollPane(table);
jframe.add(s); //下层按钮
jpanel = new JPanel(new FlowLayout(0));
jpanel.setPreferredSize(new Dimension(580, 30));
JLabel label = new JLabel("单个代理home:");
textField = new JTextField(30);
jpanel.add(label);
jpanel.add(textField); button = new JButton("启动");
button.addActionListener(this);
jpanel.add(button);
button = new JButton("停止");
button.addActionListener(this);
jpanel.add(button); jframe.add(jpanel); label = new JLabel("删除功能暂不支持");
jframe.add(label);
jframe.setVisible(true); table.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
//得到选中的行列的索引值
int r= table.getSelectedRow();
//int c= table.getSelectedColumn();
//得到选中的单元格的值,表格中都是字符串
Object value= table.getValueAt(r, 2);
textField.setText(value.toString());
/* String info=r+"行"+c+"列值 : "+value.toString();
System.out.println(info);*/
}
@Override
public void mouseReleased(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
});
} @Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
try {
if(btn.getText().equals("添加信息")){
JFileChooser fileChooser = new JFileChooser("目录选择:");
fileChooser.setFileFilter(new FileNameExtensionFilter("", "bat"));
fileChooser.setAcceptAllFileFilterUsed(true);
fileChooser.showOpenDialog(jframe);
if(null != fileChooser.getSelectedFile() ){
String filePath = fileChooser.getSelectedFile().getAbsolutePath();
//代理目录
String str = filePath.substring(0,filePath.lastIndexOf("\\"));
if(MyDao.addLineData(null, str)){
jframe.dispose();
new AgentClinet();
};
} }else if(btn.getText().equals("一键启动")){
String[][] model = tm.getModel();
for (int i = 0; i < model.length; i++) {
Runtime.getRuntime().exec("cmd /c start "+model[i][2]+"\\startup.bat");
}
}else if(btn.getText().equals("一键停止")){
String[][] model = tm.getModel();
for (int i = 0; i < model.length; i++) {
Runtime.getRuntime().exec("cmd /c start "+model[i][2]+"\\stop.bat");
}
}else if(btn.getText().equals("启动")){
Runtime.getRuntime().exec("cmd /c start "+textField.getText()+"\\startup.bat");
}else if(btn.getText().equals("停止")){
Runtime.getRuntime().exec("cmd /c start "+textField.getText()+"\\stop.bat");
}
} catch (IOException e1) {
System.exit(0);
}
} public static void main(String[] args) {
new AgentClinet();
} }

MyTabelModel.java

package com.my.agent.client;

import javax.swing.table.AbstractTableModel;

public class MyTabelModel extends AbstractTableModel{

    private static final long serialVersionUID = 1L;

    String[] tableHeader = new String[]{"序号","名称","HOME"};

    private String[][] model = null;

    public MyTabelModel(String path) {
model = MyDao.getDataFromFile(path);
} public String[][] getModel() {
return model;
} public void setModel(String[][] model) {
this.model = model;
} @Override
public int getColumnCount() {
return tableHeader.length;
} @Override
public int getRowCount() {
return model.length;
} @Override
public Object getValueAt(int row, int col) {
return model[row][col];
}
/**
* 此方法返回col位置的列的名称
*/
@Override
public String getColumnName(int column) {
return tableHeader[column];
} }

MyTabelModel.java

数据来源,放到一个txt文件里面:

package com.my.agent.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class MyDao { private static BufferedWriter bw; public static void main(String[] args) {
getDataFromFile(null);
} public static String[][] getDataFromFile(String dir){
String agentsFile = "";
if(null==dir || "".equals(dir)){
dir = System.getProperty("user.home");
}
File dirF = new File(dir);
if(!dirF.isDirectory()){
throw new RuntimeException("配置的地址不是一个目录");
}else{
agentsFile = dirF.getAbsolutePath()+"/agents.txt";
}
File file = new File(agentsFile);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
}
}
List<String> datas = getLineData(agentsFile);
String[][] ret = new String[datas.size()][];
for (int i = 0; i < datas.size(); i++) {
String[] table = ((i+1)+"&"+datas.get(i).trim()).split("&");
ret[i] = table;
}
/*
for (int i = 0; i < ret.length; i++) {
System.out.println(ret[i][0]+"=="+ret[i][1]+"==="+ret[i][2]);
}
*/
return ret;
} public static List<String> getLineData(String path) {
List<String> urls = new ArrayList<String>();
try {
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
String str = null;
while ((str = br.readLine()) != null) {
if(str!=null && str.contains("&")){
urls.add(str);
}
}
br.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return urls;
} public static boolean addLineData(String filename,String filePath){
if(filename==null){
filename = System.getProperty("user.home")+"\\agents.txt";
}
String agentName = filePath.substring(filePath.lastIndexOf("\\")+1);
try {
File file = new File(filename);
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(filename,true);
bw = new BufferedWriter(fileWriter);
bw.write(agentName+"&"+filePath);
bw.newLine();
bw.flush();
bw.close();
} catch (Throwable e) {
//do noting
return false;
}
return true;
} }

以上是第一个小工具,只是用了txt文件进行数据存储,第二个客户端按数据源分为以下两种:

1.查询MongoDB数据库:

主类:

 package com.my.mssage.client;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField; public class MessageClient implements ActionListener{ JFrame jframe = null;
JTextField pathText = null;
JTextField userNameText = null;
JTextField pwdText = null;
JTextArea textArea = null;
JButton startBtn = null;
JButton stopBtn = null;
JTextField timeText = null; boolean timerRun = false; JLabel dateLabel = new JLabel("监控时间(-至今)");
JTextField dateText = null;
boolean dateshow = false;
public MessageClient(){
jframe = new JFrame("监控");
jframe.setSize(600, 400);
jframe.setBackground(Color.BLUE);
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setLayout(new FlowLayout(0));
jframe.getContentPane().add(new JLabel("数据库ip地址:"));
pathText = new JTextField(10);
pathText.setText("");
jframe.getContentPane().add(pathText);
jframe.getContentPane().add(new JLabel("端口:"));
userNameText = new JTextField(10);
jframe.getContentPane().add(userNameText);
jframe.getContentPane().add(new JLabel("文档名称:"));
pwdText = new JTextField(10);
jframe.getContentPane().add(pwdText);
jframe.getContentPane().add(new JLabel("刷新时间(秒):"));
timeText = new JTextField(5);
timeText.setText("300");
jframe.getContentPane().add(timeText); dateText = new JTextField(10);
dateText.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
jframe.getContentPane().add(dateLabel);
jframe.getContentPane().add(dateText);
dateLabel.setVisible(dateshow);
dateText.setVisible(dateshow); JPanel panel = new JPanel(new FlowLayout(2));
startBtn = new JButton("开始监控");
startBtn.addActionListener(this);
panel.add(startBtn);
stopBtn = new JButton("停止监控");
stopBtn.addActionListener(this);
panel.add(stopBtn);
jframe.add(panel);
textArea = new JTextArea(18,53);
JScrollPane scrollPane = new JScrollPane(textArea);
jframe.add(scrollPane); timeText.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F11){
dateshow = !dateshow;
dateLabel.setVisible(dateshow);
dateText.setVisible(dateshow);
}
}
}); jframe.setVisible(true);
} public static void main(String[] args) {
new MessageClient();
} @Override
public void actionPerformed(ActionEvent e) { JButton btn = (JButton)e.getSource();
final Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
String startDate = null;
if(dateshow){
startDate = dateText.getText();
}
if(timerRun){
textArea.setText("开始监控:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\r\n");
List<WorkInfo> list = MongoDao.queryWorkitem(pathText.getText(),
userNameText.getText(), pwdText.getText(), startDate); for (WorkInfo workInfo : list) {
textArea.append(workInfo.getStopTime()+"======"+workInfo.getWorkName()+":"+workInfo.getWorkType()+":"+workInfo.getWorkState()+":"+workInfo.getStateDetail()+"\r\n");
SoundPlay.playSound(workInfo.getWorkType());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
timer.cancel();
this.cancel();
textArea.setText("停止监控......");
}
System.gc();
}
}; if(btn.getText().equals("开始监控")){
timerRun = true;
System.out.println(pathText.getText()+"=="+userNameText.getText()+"=="+pwdText.getText());
timer.schedule(task,0, Long.parseLong(timeText.getText())*1000);
}else if(btn.getText().equals("停止监控")){
timerRun = false;
System.out.println("stop......");
task.cancel();
timer.cancel();
}else{
System.out.println("......");
} }
}

数据查询:

package com.my.mssage.client;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.bson.Document; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; public class MongoDao { public static void main(String[] args) { } public static List<WorkInfo> queryWorkitem(String ip, String port, String dbname,String startDate) { List<String> list = new ArrayList<String>();
List<WorkInfo> workList = new ArrayList<WorkInfo>();
MongoClient client = new MongoClient(ip,Integer.parseInt(port));
MongoDatabase db = client.getDatabase(dbname); try {
/*
MongoIterable<String> cos = db.listCollectionNames();
for (String name : cos) {
System.out.println(name);
}
*/
MongoCollection<Document> collection = db.getCollection("nSite.wf.workitems");
if(startDate == null || "".equals(startDate)){
startDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
BasicDBObject query = new BasicDBObject();
query.put("state", "Exception");
DBObject timeQuery = new BasicDBObject();
timeQuery.put("$gt", new SimpleDateFormat("yyyy-MM-dd").parse(startDate));
timeQuery.put("$lt", new Date());
query.put("startTime", timeQuery);
FindIterable<Document> doc = collection.find(query); int i = 0; for (Document document : doc) {
Date tempDate = document.getDate("stopTime");
String stateDetail = document.getString("stateDetails");
WorkInfo workInfo = new WorkInfo(document.getString("name"),
document.getString("activityDefineName"), "出错了");
workInfo.setStopTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tempDate));
workInfo.setStateDetail(stateDetail);
workList.add(workInfo);
i++;
list.add(document.get("name")+"=="+document.get("activityDefineName")+"==出错了");
System.out.println(document.get("name")+"=="+document.get("activityDefineName")+"==出错了");
}
System.out.println(i+"===="+list.size()); } catch (Throwable e) {
throw new RuntimeException(e.getMessage());
}finally {
client.close();
}
return workList;
} }

MongoDao.java

2.查询Mysql数据库:

主类:

package com.my.mssage.client;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask; import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField; public class MysqlClient implements ActionListener{ JFrame jframe = null;
JTextField pathText = null;
JTextField portText = null;
JTextField userNameText = null;
JTextField pwdText = null;
JTextArea textArea = null;
JButton startBtn = null;
JButton stopBtn = null;
JTextField timeText = null; boolean timerRun = false; JLabel dateLabel = new JLabel("监控时间(-至今)");
JTextField dateText = null;
boolean dateshow = false; public MysqlClient(){
jframe = new JFrame("监控");
jframe.setSize(600, 400);
jframe.setBackground(Color.BLUE);
jframe.setResizable(false);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setAlwaysOnTop(true);
jframe.setLayout(new FlowLayout(0));
jframe.getContentPane().add(new JLabel("db地址:端口:"));
pathText = new JTextField(12);
pathText.setText("");
jframe.getContentPane().add(pathText);
jframe.getContentPane().add(new JLabel("db名称:"));
portText = new JTextField(5);
portText.setText("");
jframe.getContentPane().add(portText);
jframe.getContentPane().add(new JLabel("db用户名:"));
userNameText = new JTextField(5);
jframe.getContentPane().add(userNameText);
jframe.getContentPane().add(new JLabel("db密码:"));
pwdText = new JTextField(5);
jframe.getContentPane().add(pwdText);
jframe.getContentPane().add(new JLabel("刷新时间(秒):"));
timeText = new JTextField(6);
timeText.setText("300");
jframe.getContentPane().add(timeText); dateText = new JTextField(10);
dateText.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
jframe.getContentPane().add(dateLabel);
jframe.getContentPane().add(dateText);
dateLabel.setVisible(dateshow);
dateText.setVisible(dateshow); JPanel panel = new JPanel(new FlowLayout(2));
startBtn = new JButton("开始监控");
startBtn.addActionListener(this);
panel.add(startBtn);
stopBtn = new JButton("停止监控");
stopBtn.addActionListener(this);
panel.add(stopBtn);
jframe.add(panel);
textArea = new JTextArea(18,53);
JScrollPane scrollPane = new JScrollPane(textArea);
jframe.add(scrollPane);
timeText.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_F11){
dateshow = !dateshow;
dateLabel.setVisible(dateshow);
dateText.setVisible(dateshow);
}
}
}); jframe.setVisible(true);
} public static void main(String[] args) {
new MysqlClient();
} @Override
public void actionPerformed(ActionEvent e) { JButton btn = (JButton)e.getSource(); final Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
String startDate = null;
if(dateshow){
startDate = dateText.getText();
}
if(timerRun){ textArea.setText("开始监控:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\r\n");
List<WorkInfo> list = NSDao.query(pathText.getText(), portText.getText(),
userNameText.getText(), pwdText.getText(), startDate);
for (WorkInfo workInfo : list) {
textArea.append(workInfo.getStopTime()+"======"+workInfo.getWorkName()+":"+workInfo.getWorkType()+":"+workInfo.getWorkState()+":"+workInfo.getStateDetail()+"\r\n");
SoundPlay.playSound(workInfo.getWorkType());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}else{
timer.cancel();
this.cancel();
textArea.setText("停止监控......");
}
System.gc();
}
}; if(btn.getText().equals("开始监控")){
timerRun = true;
System.out.println(pathText.getText()+"=="+userNameText.getText()+"=="+pwdText.getText());
timer.schedule(task,0, Long.parseLong(timeText.getText())*1000);
}else if(btn.getText().equals("停止监控")){
timerRun = false;
System.out.println("stop......");
task.cancel();
timer.cancel();
}else{
System.out.println("......");
}
}
}

数据来源:

package com.my.mssage.client;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; public class NSDao { private static String driver = "com.mysql.jdbc.Driver"; static Connection conn = null;
static Statement statement = null;
static PreparedStatement ps = null;
static ResultSet rs = null;
static List<String> list = null; /*
* 连接数据库
*/
public static void connDB(String iport,String dbname,String user,String pwd) {
try {
String url = "jdbc:mysql://"+iport+"/"+dbname;
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, user, pwd);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to MySQL!");
} statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
} /*
* 关闭数据库
*/
public static void closeDB() {
if(rs != null ){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null){
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} /*
* 查询数据表
*/
public static List<WorkInfo> query(String iport,String dbname,String user,String pwd,String startDate){
List<WorkInfo> workList = new ArrayList<WorkInfo>();
connDB(iport,dbname,user,pwd);
if(startDate == null || "".equals(startDate)){
startDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
}
list = new ArrayList<String>();//返回所有记录
String str_query = "";
try {
String sql = "select workname,activitydefname,stoptime,statedetail,createtime from wf_workitem where currentstate = 5 "
+ " and "
+" starttime BETWEEN '"
+startDate
+"' and '"
+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())
+"' order by createtime";
rs = statement.executeQuery(sql);
while(rs.next()){
WorkInfo workInfo = new WorkInfo(rs.getString(1),
rs.getString(2), "出错了");
workInfo.setStopTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rs.getDate(3)));
workInfo.setStateDetail(rs.getString(4));
str_query = rs.getString(1)+"=="+rs.getString(2)+"==出错了";
System.out.println(str_query);
list.add(str_query);
workList.add(workInfo);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeDB();
}
return workList;
} public static void main(String[] args) {
List<WorkInfo> workList = null;
for (int i = 0; i < workList.size(); i++) {
System.out.println(list.get(i));
}
} }

3、共有代码bean:

package com.my.mssage.client;

public class WorkInfo {

    private String workName;

    private String workType;

    private String workState;

    private String stopTime;

    private String stateDetail;

    public WorkInfo(String workName,String workType,String workState,
String stopTime,String stateDetail){
this.workName = workName;
this.workType = workType;
this.workState = workState;
this.stopTime = stopTime;
this.stateDetail = stateDetail;
} public WorkInfo(String workName,String workType,String workState){
this.workName = workName;
this.workType = workType;
this.workState = workState;
} public String getStopTime() {
return stopTime;
} public void setStopTime(String stopTime) {
this.stopTime = stopTime;
} public String getStateDetail() {
return stateDetail;
} public void setStateDetail(String stateDetail) {
this.stateDetail = stateDetail;
} public String getWorkName() {
return workName;
} public void setWorkName(String workName) {
this.workName = workName;
} public String getWorkType() {
return workType;
} public void setWorkType(String workType) {
this.workType = workType;
} public String getWorkState() {
return workState;
} public void setWorkState(String workState) {
this.workState = workState;
} }

声音播放:

package com.my.mssage.client;

import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine; public class SoundPlay { public static void playSound(String type){ try {
String dir = System.getProperty("user.home")+"\\jkaudio";
File file = new File(dir);
switch (type) {
case "打包":
file = new File(dir+"\\1.wav");
break;
case "送演播室":
file = new File(dir+"\\2.wav");
break;
case "审片回退通知":
file =new File(dir+"\\3.wav");
break;
case "自动技审":
file =new File(dir+"\\4.wav");
break;
case "TitleToEditsphere":
file =new File(dir+"\\5.wav");
break;
default:
file = new File(dir+"\\6.wav");
break;
} System.out.println(file.getAbsolutePath());
AudioInputStream cin = AudioSystem.getAudioInputStream(file);
AudioFormat format = cin.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);//或者line.open();format参数可有可无
line.start();
int nBytesRead = 0;
byte[] buffer = new byte[512];
while (true) {
nBytesRead = cin.read(buffer, 0, buffer.length);
if (nBytesRead <= 0)
break;
line.write(buffer, 0, nBytesRead);
}
line.drain();
line.close();
} catch (Throwable e) {
System.out.println(e.getMessage());
// do noting
e.printStackTrace();
}
} public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
playSound(i+"");
}
} }

以上!