在java中,如何倒着读取读文本
思路大概是这样的,我想从文本文件最后一行读取,读取5000个字节就ok,怎么写,我的代码是顺着读取,求大神修改作品,谢谢!classDBhandle{publicvoid...
思路大概是这样的,我想从文本文件最后一行读取,读取5000个字节就ok,怎么写,我的代码是顺着读取,求大神修改作品,谢谢!
class DBhandle{
public void getText(){ String str1 = "123";
File fil = new File("E:/test/" + str1 +".txt");
FileReader fr = null; char[] buf = new char[1000]; try{ fr = new FileReader(fil);
int len = fr.read( buf,0,(int)fil.length() ); String context =
new String(buf); int start = 0, end = 0; String temp1 = null; String temp2 = null;
//indexOf允许你判断一个字串是否存在於一个更长的字串中以及它所处的位置
while( (end = context.indexOf(13,start) ) >= 0){
//charAt函数返回在字串中字元处在给定的位置
if( context.charAt(end + 1) == 10){
temp1 = context.substring(start,end);
temp2 = context.substring(end + 2 );
JdAdapter db =
new JdAdapter();
db.executeQuery(temp1);
//System.out.println(temp1);
context = temp2;
}start = 0;}}catch(Exception e){ System.out.println(e.toString()); }}}
class JdAdapter {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=JasFtpDB";
private static final String USERNAME = "sa";
private static final String PASSWORD = "f1328343.";
static {try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
public Connection getConnection() {
Connection conn = null;
try{
conn=DriverManager.getConnection(
URL, USERNAME, PASSWORD); }
catch(SQLException e){ e.printStackTrace(); } return conn; }
public void close(ResultSet rs, PreparedStatement ps, Connection conn) {
if(rs!=null){ try{
rs.close(); rs=null; }catch(SQLException e){
e.printStackTrace(); } }
if(ps!=null){
try{ ps.close();
ps=null; }
catch(SQLException e){ e.printStackTrace(); } }
if(conn!=null){
try{
conn.close();
conn=null; }
catch(SQLException e){
e.printStackTrace();
} } }
public boolean executeQuery(String str1)
{
Connection conn =null;
String str;
try{
java.sql.PreparedStatement ps;
conn = DriverManager.getConnection(
URL, USERNAME, PASSWORD);
str =
"insert into mine ([Content]) values(?)";
ps = conn.prepareStatement(str);
ps.setString(1,str1);
ps.execute();
conn.commit();
ps.close();
conn.close();
System.out.println("exec ok!"); } catch(SQLException e){ System.err.println(e.getMessage()); return false; }return true; }}
public
主函数类
class TestMain {
public static void main(String[] args)throws IOException {
DBhandle handl = new DBhandle(); handl.getText();}
程式是没问题的,创建一个文本文件,如何从最后一行开始往上读取,插入数据库中,我的数据库表是这样的:
create table mine
(id int identity(1,1) primary key,
[Content] varchar(max),
Createted datetime default getdate()
)
createtable mine
(id int identity(1,1) primary key, [Content] varchar(max),
Createted datetime default getdate() ) 展开
class DBhandle{
public void getText(){ String str1 = "123";
File fil = new File("E:/test/" + str1 +".txt");
FileReader fr = null; char[] buf = new char[1000]; try{ fr = new FileReader(fil);
int len = fr.read( buf,0,(int)fil.length() ); String context =
new String(buf); int start = 0, end = 0; String temp1 = null; String temp2 = null;
//indexOf允许你判断一个字串是否存在於一个更长的字串中以及它所处的位置
while( (end = context.indexOf(13,start) ) >= 0){
//charAt函数返回在字串中字元处在给定的位置
if( context.charAt(end + 1) == 10){
temp1 = context.substring(start,end);
temp2 = context.substring(end + 2 );
JdAdapter db =
new JdAdapter();
db.executeQuery(temp1);
//System.out.println(temp1);
context = temp2;
}start = 0;}}catch(Exception e){ System.out.println(e.toString()); }}}
class JdAdapter {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static final String URL = "jdbc:sqlserver://localhost:1433;databaseName=JasFtpDB";
private static final String USERNAME = "sa";
private static final String PASSWORD = "f1328343.";
static {try { Class.forName(DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
public Connection getConnection() {
Connection conn = null;
try{
conn=DriverManager.getConnection(
URL, USERNAME, PASSWORD); }
catch(SQLException e){ e.printStackTrace(); } return conn; }
public void close(ResultSet rs, PreparedStatement ps, Connection conn) {
if(rs!=null){ try{
rs.close(); rs=null; }catch(SQLException e){
e.printStackTrace(); } }
if(ps!=null){
try{ ps.close();
ps=null; }
catch(SQLException e){ e.printStackTrace(); } }
if(conn!=null){
try{
conn.close();
conn=null; }
catch(SQLException e){
e.printStackTrace();
} } }
public boolean executeQuery(String str1)
{
Connection conn =null;
String str;
try{
java.sql.PreparedStatement ps;
conn = DriverManager.getConnection(
URL, USERNAME, PASSWORD);
str =
"insert into mine ([Content]) values(?)";
ps = conn.prepareStatement(str);
ps.setString(1,str1);
ps.execute();
conn.commit();
ps.close();
conn.close();
System.out.println("exec ok!"); } catch(SQLException e){ System.err.println(e.getMessage()); return false; }return true; }}
public
主函数类
class TestMain {
public static void main(String[] args)throws IOException {
DBhandle handl = new DBhandle(); handl.getText();}
程式是没问题的,创建一个文本文件,如何从最后一行开始往上读取,插入数据库中,我的数据库表是这样的:
create table mine
(id int identity(1,1) primary key,
[Content] varchar(max),
Createted datetime default getdate()
)
createtable mine
(id int identity(1,1) primary key, [Content] varchar(max),
Createted datetime default getdate() ) 展开
1个回答
展开全部
关键是用 RandomAccessFile,给你个例子,自己改.不会的百度RandomAccessFile即可.
package randomRead;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
//随机读取文件
public class RandomTest {
public static void main(String[] args) {
RandomTest t = new RandomTest();
File src = new File("d:\\holen.xml");
File dest = new File("d:\\copy.txt");
t.copy(src, dest, 0, 8);
}
public void copy(File src, File dest, int start, int end) {
try {
RandomAccessFile in = new RandomAccessFile(src, "r"); //读取文件
RandomAccessFile out = new RandomAccessFile(dest, "rw"); //写文件
int buf_size = 1024;
byte[] buf = new byte[buf_size];
int out_end = (int) out.length(); // 追加到文件结尾
while (start < end) {
int len = end - start;
if (len > buf_size) {
len = buf_size;
}
in.seek(start); //指向源文件的开始位置
in.read(buf, 0, len); //把文件内容读入缓冲区
out.seek(out_end); //指向目标文件的结束位置
out.write(buf, 0, len); //把文件内容写入缓冲区
start += len;
out_end += len;
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询