String[] gettxt=new String[255];
int filepointer,filesize,num = 0;
try
{
fff =new RandomAccessFile(fn,action);
filesize = fff.length();
System.out.println("File size :"+ filesize);
while ( filesize > filepointer)
{
filepointer = fff.getFilePointer();
gettxt[num] = fff.readLine();
System.out.println("filepointer : "+filepointer+" "+sss);
num = num + 1;
}
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
int filepointer,filesize,num = 0;
try
{
fff =new RandomAccessFile(fn,action);
filesize = fff.length();
System.out.println("File size :"+ filesize);
while ( filesize > filepointer)
{
filepointer = fff.getFilePointer();
gettxt[num] = fff.readLine();
System.out.println("filepointer : "+filepointer+" "+sss);
num = num + 1;
}
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
解决方案 »
- 问:如何自定义构建一个工作于阻塞模式的InputStream? - 熟悉Java通信和操作系统的进
- OAViewRowImpl,OAViewObjectImpl 引入有错吗
- 好心人帮帮我,用Java模拟HTTP Request上传Excel文件。
- 发个帖子希望大家说一下你遇到编成中容易被人忽视的细节、代码中隐患或者提高代码效率的问题
- try catch finally 执行顺序?
- 多线程问题
- 我才学5天这个问题难不倒你吧
- 【客户端不能向服务器端写文件是什么原因啊,急啊,大侠帮忙!!!!】
- 请教!DAO和ORM有什么关系?
- 我这样注册的环境变量没理由不对呀。。
- 使用log4j生成的.log文件,前缀中是不是必须含有当日日期?
- jtable中的一个列的内容过长,能不能让其折行显示!!!急!!!
readtxt myread=new readtxt();
myread.getfiletxt("d:\\a.txt");就在这返回的是一个数组了,这里开始要如何做了呢
String[] toUse=myread.getfiletxt("d:\\a.txt");
//例子
for(int i=0;i<toUse.length;i++){
System.out.println(toUse[i]);
}
D:\Work>javac readtxtservlet.java
readtxtservlet.java:26: 不兼容的类型
发现:java.lang.String
需要:java.lang.String[]
String[] toUse=myread.getfiletxt("d:\\a.txt");
^
while(file.getFilePointer<file.length())
//应为
while(file.getFilePointer()<file.length())
//另外读文本结束判断有问题(虽然不报错)
//请注意JAVA的规范。类名用大写开头!
____________________________________________________________________________
import java.io.*;
public class readtxt
{
String getfilepath;
public readtxt(String strFileName)
{
getfilepath = strFileName;
}
public String[] getfiletxt()
{
if (getfilepath==null) return new String[]{""};
File fn=new File(getfilepath);
String[] gettxt = new String[255];
int i=0;
if(fn.isFile()&&fn.canRead())
{
try
{
RandomAccessFile file=new RandomAccessFile(fn,"r");
while(file.getFilePointer() < file.length())
{
gettxt[i]=file.readLine();
i=i+1;
}
file.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return gettxt;
}
public static void main(String args[])
{
if(args.length < 1)
{
System.out.println("Please input the file name.");
System.exit(0);
}
readtxt oGetFileTxt = new readtxt(args[0]);
String[] strArrayGetFileTxt = oGetFileTxt.getfiletxt();
System.out.println("strArrayFileTxt[0] = " + strArrayGetFileTxt[0]);
System.out.println("strArrayFileTxt[1] = " + strArrayGetFileTxt[1]);
}
}
_____________________________________________________________________________
import java.io.*;
public class readtxt
{
public String[] getfiletxt(String getfilepath)
{
try
{
if (getfilepath==null) return new String[]{""};
File fn=new File(getfilepath);
String[] gettxt=new String[255];
int i=0;
if(fn.isFile()&&fn.canRead())
{
RandomAccessFile file=new RandomAccessFile(fn,"r");
while(file.getFilePointer()<file.length())
{
gettxt[i]=file.readLine();
i=i+1;
}
file.close();
return gettxt;
}
}
catch(Exception e)
{
e.printStackTrace();
}
} }
catch(Exception e)
{
e.printStackTrace();}
应该返回
如
catch(Exception e)
{
e.printStackTrace();
return null;
}
看下面的代码://注释所说
if (fn.isFile() && fn.canRead())
{ RandomAccessFile file = new RandomAccessFile(fn, "r");
while (file.getFilePointer() < file.length())
{
gettxt[i] = file.readLine();
i = i + 1;
}
file.close();
return gettxt;
}
//here should return
}
catch (Exception e)
{
e.printStackTrace();
//here should return
}
public String Fmt_Value(double Value)throws Exception有什么区别呢,static主要作用是什么,: ycats(加菲猫) 帮我答,我马上加分了,谢谢
如:public static String Fmt_Value(double Value)throws Exception
可以:ClassName.Fmt_Value(double Value)
public String Fmt_Value(double Value)throws Exception
必须:
new ClassName().Fmt_Value(double Value)
请看看Java的基本书籍