当前位置: 技术问答>java相关
头痛问题:编译时出错,FileOutputStream类write方法错误
来源: 互联网 发布时间:2015-02-16
本文导语: 下面是一段源程序,要实现通过按DigData按钮从sqlserver数据库里提取数据,再把数据写进一个文本文件中存在与源程序相同的目录下,编译的时候出现1个错误(已经标在源程序旁边)FileOutputStream类中的方法write错误,...
下面是一段源程序,要实现通过按DigData按钮从sqlserver数据库里提取数据,再把数据写进一个文本文件中存在与源程序相同的目录下,编译的时候出现1个错误(已经标在源程序旁边)FileOutputStream类中的方法write错误,无法解决,还请高手指教:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
public class Prc_c_m extends JFrame implements ActionListener{
JButton buttonDigdata=new JButton("Dig Data");
JButton buttonUpload=new JButton("UpLoad File");
public Prc_c_m() {
super("Prc_c");
setSize(360,360);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 2, 5, 15));
buttonDigdata.addActionListener(this);
pane.add(buttonDigdata);
pane.add(buttonUpload);
setContentPane(pane);
}
void digData() {
String data = "jdbc:odbc:prc_c";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(
data, "sa", "");
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * FROM TablePl WHERE Name='a' ORDER BY Id");
try {
FileOutputStream file = new FileOutputStream("prc.txt");
int i=0;
while (rec.next()){
file.write(rec.getString(++i)); //不能解析的符号,java.io.FileOutputStream类中的方法write错误
}
file.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() ==buttonDigdata)
digData();
}
public static void main(String[] arguments) {
Prc_c_m frame = new Prc_c_m();
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.io.*;
public class Prc_c_m extends JFrame implements ActionListener{
JButton buttonDigdata=new JButton("Dig Data");
JButton buttonUpload=new JButton("UpLoad File");
public Prc_c_m() {
super("Prc_c");
setSize(360,360);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(1, 2, 5, 15));
buttonDigdata.addActionListener(this);
pane.add(buttonDigdata);
pane.add(buttonUpload);
setContentPane(pane);
}
void digData() {
String data = "jdbc:odbc:prc_c";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection(
data, "sa", "");
Statement st = conn.createStatement();
ResultSet rec = st.executeQuery(
"SELECT * FROM TablePl WHERE Name='a' ORDER BY Id");
try {
FileOutputStream file = new FileOutputStream("prc.txt");
int i=0;
while (rec.next()){
file.write(rec.getString(++i)); //不能解析的符号,java.io.FileOutputStream类中的方法write错误
}
file.close();
} catch (IOException e) {
System.out.println("Error -- " + e.toString());
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString()
+ e.getMessage());
}
}
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() ==buttonDigdata)
digData();
}
public static void main(String[] arguments) {
Prc_c_m frame = new Prc_c_m();
frame.pack();
frame.setVisible(true);
}
}
|
java.io.FileOutputStream 无 write(String)
有 write(byte[] )
write(byte[] b, int off, int len)
write(int b)
改为try {
FileOutputStream file = new FileOutputStream("prc.txt",true);
int i=0;
while (rec.next()){
file.write(rec.getString(++i).getBytes() ); //不能解析的符号,java.io.FileOutputStream类中的方法write错误
}
有 write(byte[] )
write(byte[] b, int off, int len)
write(int b)
改为try {
FileOutputStream file = new FileOutputStream("prc.txt",true);
int i=0;
while (rec.next()){
file.write(rec.getString(++i).getBytes() ); //不能解析的符号,java.io.FileOutputStream类中的方法write错误
}