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文件(一整個獲取)

