java read into_在java中,如何倒着读取读文本

思路大概是这样的,我想从文本文件最后一行读取,读取5000个字节就ok,怎么写,我的代码是顺着读取,求大神修改作品,谢谢!classDBhandle{publicvoidgetText(){Stringstr1="123";Fil...

思路大概是这样的,我想从文本文件最后一行读取,读取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() )

展开

要将Java的一张图片生成为二维码,可以使用ZXing库。以下是一个示例代码: ```java import java.awt.image.BufferedImage; import java.io.File; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class ImageToQRCode { public static void main(String[] args) throws Exception { String imagePath = "path/to/image.jpg"; String qrCodePath = "path/to/qrcode.png"; String qrCodeText = "This is the text to be encoded into QR code."; // Read the image BufferedImage image = ImageIO.read(new File(imagePath)); // Encode the text into QR code QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, image.getWidth(), image.getHeight(), hints); // Convert the QR code into an image int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage qrCodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { qrCodeImage.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } // Combine the image and the QR code BufferedImage combinedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); combinedImage.getGraphics().drawImage(image, 0, 0, null); combinedImage.getGraphics().drawImage(qrCodeImage, (image.getWidth() - width) / 2, (image.getHeight() - height) / 2, null); // Save the combined image ImageIO.write(combinedImage, "png", new File(qrCodePath)); } } ``` 这段代码将会读取指定路径下的图片,将指定文本编码为二维码,并将二维码插入到图片央,最后将生成的图片保存为PNG格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值