java运行txt
㈠ 缂栧啓java 绋嫔簭 镓揿紑TXT鏂囦欢
浣犵殑txt鏂囦欢钖嶅瓧鍙00.txt锛屾斁鍦╟鐩樻牴鐩褰曘傚唴瀹瑰啓鎴愶细
a
b
c
d
e
鎴栬咃细abcde銆傛垜缁欎綘鍐欎简涓锛岃繍琛屾垚锷熶简锛
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class findFile {
public String loadAFileToStringDE(String path) throws IOException {
ArrayList Arry = new ArrayList();
File f = new File(path);
BufferedReader br = null;
String ret = null;
System.out.println(f);
try {
br = new BufferedReader(new FileReader(f));
String line = null;
while ((line = br.readLine()) != null) {
Arry.add(line);
}
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
}
}
}
String temp = (String)Arry.get(Arry.size()-1);
System.out.println("链钖庝竴涓瀛楁瘝锛"+temp.substring(temp.length()-1,temp.length()));
return (String)Arry.get(Arry.size()-1);
}
public static void main(String[] args) {
findFile files= new findFile();
try{
System.out.println("链钖庝竴琛"+files.loadAFileToStringDE("c:/00.txt"));
}catch(IOException io){}
}
}
缁撴灉锛
c:\00.txt
链钖庝竴涓瀛楁瘝锛欤
链钖庝竴琛宔
㈡ java中如何调用txt里的数据
创建一个数据读取流
下面是一个把图片存到数据库的例子,你可以参考一下:
public class FileUtil {
private static Log log = LogFactory.getLog(FileUtil.class);
//将文件对象转换为二进制字节数组
public static byte[] toByteArray(File photo) throws IOException {
//获得文件对象的输入流
FileInputStream fis = new FileInputStream(photo);
BufferedInputStream bis = new BufferedInputStream(fis);
//字节数组的输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = bis.read();
while (c != -1) {
baos.write(c);
c = bis.read();
}
bis.close();
byte[] rtn = baos.toByteArray();
baos.close();
return rtn;
}
/**
* 构建小样图片
* @param srcFile
* @return
*/
public static byte[] buildThumbnail(File srcFile) {
byte[] rtn = null;
try {
Image src = ImageIO.read(srcFile); // 构造Image对象
int oldWidth = src.getWidth(null); // 得到源图宽
int oldHeight = src.getHeight(null);// 得到源图高
log.debug("old width is " + oldWidth);
log.debug("old height is " + oldHeight);
float divWidth = 200f; // 限制宽度为200
int newWidth = 200; // 缩略图宽,
int newHeight = 0; // 缩略图高
float tmp;//缩略比例
if (oldWidth > newWidth) {
tmp = oldWidth / divWidth;
newWidth = Math.round(oldWidth / tmp);// 计算缩略图高
newHeight = Math.round(oldHeight / tmp);// 计算缩略图高
log.debug("tmp scale is " + tmp);
} else {
newWidth = oldWidth;
newHeight = oldHeight;
}
//绘制的图片默认大小
int imageHeight = 100;
int imageWidth = 200;
log.debug("new width is " + newWidth);
log.debug("new height is " + newHeight);
BufferedImage bufferedImage = new BufferedImage(newWidth,
newHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = (Graphics2D) bufferedImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.clearRect(0, 0, imageWidth, imageHeight);
//绘制新的图片对象
bufferedImage.getGraphics().drawImage(src,
//(imageWidth - oldWidth) / 2, (imageHeight - newHeight) / 2,
0,0,
newWidth, newHeight, null); // 绘制缩小后的图
ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(baos);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);
encoder.encode(bufferedImage); // 进行JPEG编码
rtn = baos.toByteArray();
bos.close();
baos.close();
} catch (Exception e) {
log.debug(e);
} finally {
}
return rtn;
}
}
㈢ java怎样实现读写TXT文件
主要有用到java原生态的Io类,没有第三个包。直接上代码:
importjava.io.*;
publicclasswrite{
publicstaticvoidmain(String[]args){
write("E://123.txt","hello");
}
publicstaticvoidwrite(Stringpath,Stringcontent){
try{
Filef=newFile(path);
if(f.exists()){
System.out.println("文件存在");
}else{
System.out.println("文件不存在,正在创建...");
if(f.createNewFile()){
System.out.println("文件创建成功!");
}else{
System.out.println("文件创建失败!");
}
}
BufferedWriteroutput=newBufferedWriter(newFileWriter(f));
output.write(content);
output.close();
}catch(Exceptione){
e.printStackTrace();
}
}
}
㈣ 如何在Java中打开TXT文件
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
publicclassReadText{
publicstaticvoidmain(String[]args)
{
readTextContent();
}
()
{
try
{
Filefile=newFile("E://test.txt");
FileInputStreamfis=newFileInputStream(file);
Stringstr="";
byte[]bytes=newbyte[1024];
intlength=0;
while((length=fis.read(bytes))!=-1)
{
str+=newString(bytes,0,length);
}
System.out.println(str);
}catch(FileNotFoundExceptione)
{
e.printStackTrace();
}catch(IOExceptione)
{
e.printStackTrace();
}
}
}
㈤ java如何读取txt文件
读取txt文件(一整个获取)