读取文件问题 :在线等待 从文件读出数据,放为VECTOR,或者hash*类的东西,修改后再覆盖原来的~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用java.io.RandomAccessFile。RandomAccessFile(File file, String mode),mode为"rw",可以以追加模式打开文件。seek(long l)可以实现文件光标定位。 同意楼上的方法,不过建议把每行都应该是相等的字符,这样用seek()就比较方便定位在某一行上。不够的可以补空格,读取的时候可以用StringTokenize类进行出去,并用trim()去掉多余空格。 lixiang823517大哥: 不会吧,取出来再放回去,如果我有很多数据呢? Danger2000(飞鱼)大哥,能否给点定位的代码就像,将文件指针定位于23的2的位置,谢谢大哥了 to rainboy20024(鹰鹰) :long lineLength = 每行的长度;long nameLength = 姓名部分的长度;in.seek(lineLength * 2 + nameLength +1); //把指针定位在23的2的位置前 可以跳过已经写入的文件长度再写试试import java.io.*;public class myFile{ public myFile() { init(); RecordAppend("xuguo"); } public void init() { try { String Record="zhoukun"; String myFilePath="D:/jspServer"; String myFileName="myWriteFile.txt"; File dir=new File(myFilePath,"newFile"); dir.mkdir(); File f=new File(dir,myFileName); FileOutputStream o=new FileOutputStream(f); BufferedOutputStream myin=new BufferedOutputStream(o); byte c[]=Record.getBytes(); myin.write(c); myin.flush(); myin.close(); } catch(IOException e) { System.out.println(e); } } public void RecordAppend(String strAppend) { try { int filelength; int writelength; String myFilePath="D:/jspServer/newFile"; String myFileName="myWriteFile.txt"; File f=new File(myFilePath,myFileName); FileOutputStream o=new FileOutputStream(f); BufferedOutputStream myin=new BufferedOutputStream(o); byte c[]=strAppend.getBytes(); filelength=(int)f.length(); writelength=strAppend.length(); myin.write(c,filelength,writelength); myin.flush(); myin.close(); } catch(IOException e) { System.out.println(e); } } public static void main(String args[]) { new myFile(); }} 谢谢大家,还麻烦大家一下: 如果我要用文件进行存储,并在图形界面进行查询,譬如:我要查询"小唐"该怎么找啊,用VECTOR,或者 hash* 之类吗?还是读取每行,并进行切割,比较? quartz 时间表达式 struts2.3能否在JDK1.5中运行,求大神解答下,感激不尽 J2SE--重写color类获得自己想要的任何颜色 问一个排序的算法? Random中nextInt问题 刚才java培训班学完,去什么样的公司好混呢? 新手请教,一些简单的问题!! 读文件绝对路径和相对路径问题 求解类型信息相关问题 谢谢大神 请问哪里有java核心技术卷1 的电子书下载? 谢谢! 初学,超简单问题,别笑我! 联接Oracle数据库出现NullPointerException错误?
RandomAccessFile(File file, String mode),mode为"rw",可以以追加模式打开文件。
seek(long l)可以实现文件光标定位。
不会吧,取出来再放回去,如果我有很多数据呢?
long nameLength = 姓名部分的长度;
in.seek(lineLength * 2 + nameLength +1); //把指针定位在23的2的位置前
import java.io.*;public class myFile
{
public myFile()
{
init();
RecordAppend("xuguo");
}
public void init()
{
try
{
String Record="zhoukun";
String myFilePath="D:/jspServer";
String myFileName="myWriteFile.txt";
File dir=new File(myFilePath,"newFile");
dir.mkdir();
File f=new File(dir,myFileName);
FileOutputStream o=new FileOutputStream(f);
BufferedOutputStream myin=new BufferedOutputStream(o);
byte c[]=Record.getBytes();
myin.write(c);
myin.flush();
myin.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
public void RecordAppend(String strAppend)
{
try
{
int filelength;
int writelength;
String myFilePath="D:/jspServer/newFile";
String myFileName="myWriteFile.txt";
File f=new File(myFilePath,myFileName);
FileOutputStream o=new FileOutputStream(f);
BufferedOutputStream myin=new BufferedOutputStream(o);
byte c[]=strAppend.getBytes();
filelength=(int)f.length();
writelength=strAppend.length();
myin.write(c,filelength,writelength);
myin.flush();
myin.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
public static void main(String args[])
{
new myFile();
}
}
如果我要用文件进行存储,并在图形界面进行查询,譬如:我要查询"小唐"该怎么找啊,用VECTOR,或者 hash* 之类吗?还是读取每行,并进行切割,比较?