报错在类中找不到主方法, 请将主方法定义为: public static void main(String[] args) 代码里面要怎么改
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;...
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class sss {
File f=new File("d://","test.txt");
FileInputStream fileInputStream;
FileOutputStream fileOutputStream;
public String read(){
String s=null;
try {
int size=0;
byte[] buffer=new byte[1024];
fileInputStream=new FileInputStream(f);
while((size=fileInputStream.read(buffer))!=-1){
s=new String(buffer,"utf-8");
System.out.println(s);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return s;
}
public Boolean write(String str){
try {byte[] userbname=str.getBytes("utf-8");
fileOutputStream=new FileOutputStream(f,true);
fileOutputStream.write(userbname,0, userbname.length);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}public void a(){
this.write("Welcome to Java world");
this.read();
}
} 展开
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class sss {
File f=new File("d://","test.txt");
FileInputStream fileInputStream;
FileOutputStream fileOutputStream;
public String read(){
String s=null;
try {
int size=0;
byte[] buffer=new byte[1024];
fileInputStream=new FileInputStream(f);
while((size=fileInputStream.read(buffer))!=-1){
s=new String(buffer,"utf-8");
System.out.println(s);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} return s;
}
public Boolean write(String str){
try {byte[] userbname=str.getBytes("utf-8");
fileOutputStream=new FileOutputStream(f,true);
fileOutputStream.write(userbname,0, userbname.length);
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}public void a(){
this.write("Welcome to Java world");
this.read();
}
} 展开
1个回答
2018-01-10 · 百度知道合伙人官方认证企业
尚学堂科技有限公司
北京尚学堂科技有限公司是一家从事JAVA、大数据、web前端、网络在线教育、互联网架构师、Android、ios技术开发、咨询为一体的软件公司。公司由海外留学人员和国内IT人士创建。
向TA提问
关注
展开全部
一个类中需要有程序执行的入口,例如main方法。把a()方法换成main方法,其他方法没问题如下:
public class Sss {
File f = new File("d://", "test.txt");
FileInputStream fileInputStream;
FileOutputStream fileOutputStream;
public String read()() {
String s = null;
try {
int size = 0;
byte[] buffer = new byte[1024];
fileInputStream = new FileInputStream(f);
while ((size = fileInputStream.read(buffer)) != -1) {
s = new String(buffer, "utf-8");
System.out.println(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return s;
}
public Boolean write(String str) {
try {
byte[] userbname = str.getBytes("utf-8");
fileOutputStream = new FileOutputStream(f, true);
fileOutputStream.write(userbname, 0, userbname.length);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true;
}
public static void main(String[] args) {
Sss s1 = new Sss();
s1.write("Welcome to Java world");
s1.read();
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询