輸入無需使用位元組流,直接字元流讀取即可。
privatevoidinput(StringfileName)throwsIOException{
try(BufferedReaderreader=newBufferedReader(newFileReader(fileName))){
Stringline;
while((line=reader.readLine())!=null){
System.out.println(line);
}
}
}
同樣輸出,只要把Input換成Output;
privatevoidoutput(StringfileName,Stringcontent)throwsIOException{
try(BufferedWriterwriter=newBufferedWriter(newFileWriter(fileName))){
writer.write(content);
writer.flush();
}
}
⑵ java把運行結果輸出到txt
這個最主要的就是萬年歷演算法,網上一搜就有 輸出保存到新建的Txt文件很容易就是IO寫入操作例如:
BufferedWriterwriter=newBufferedWriter(newFileWriter(txt文件路徑加路徑名,
false));//true表示追加
writer.write(要寫入的數據);
writer.close();
⑶ JAVA如何調用txt文件 並用txt列印輸出結果
import java.io.*;
public class Start
{
public void readAndRead()
{
try
{
FileReader fr = new FileReader("need.txt");//需要讀取的文件路徑
BufferedReader br = new BufferedReader(fr);
String s = br.readLine();
while(s!=null)//如果當前行不為空
{
System.out.println(s);//列印當前行
s= br.readLine();//讀取下一行
}
br.close();//關閉版BufferReader流
fr.close(); //關閉文件流
}catch(IOException e)//捕捉異常權
{
System.out.println("指定文件不存在");//處理異常
}
}
public static void main(String [] args)
{
Start start = new Start();
start.readAndRead();
}
}
⑷ java輸出txt
在D盤新建兩個文件test.txt,test1.txt
把內容拷到test中,test1為輸出。。。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ZhiDao {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
String lineContent = null;
StringBuffer sb = new StringBuffer();
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("D:\\test.txt"));
pw = new PrintWriter("D:\\test1.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((lineContent = br.readLine()) != null) {
if(lineContent.equals("")){
sb.append("\r\n");
sb.append("\r\n");
}else{
for(int i = 0; i<lineContent.length(); i++){
char charContent = lineContent.charAt(i);
sb.append(charContent);
if(i != 0 && i%60 == 0){
sb.append("\r\n");
}
}
}
}
System.out.println(sb);
pw.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⑸ Java讀取TXT文件並且輸出~請各位大俠解答。小弟拜謝
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.PrintWriter;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
publicclassReader{
publicstaticvoidmain(String[]args)throwsFileNotFoundException{
Scannerin=newScanner(newFile("dataIn.txt"));
Stringline;
//每次讀取一行
List<String>result=newArrayList<String>();
while(in.hasNextLine()){
line=in.nextLine();
Stringstr[]=line.split(";");
result.add(str[1].split("=")[1]);
}
FiledataOut=newFile("dataOut.txt");
PrintWriterpw=newPrintWriter(dataOut);
for(Stringb:result){
System.out.println(b);
pw.println(b);
}
pw.close();
}
}
⑹ JAVA 如何輸出數據到TXT文件內
package test;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
public class ReadColorTest {
/**
* 讀取一張圖片的RGB值
*
* @throws Exception
*/
public void getImagePixel(String image) throws Exception {
File fileCar = new File("D:\\car.txt");
FileOutputStream fos = new FileOutputStream(fileCar);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int[] rgb = new int[3];
File file = new File(image);
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
System.out.println("width=" + width + ",height=" + height + ".");
bos.write(("width=" + width + ",height=" + height + ".\n").getBytes());
System.out.println("minx=" + minx + ",miniy=" + miny + ".");
bos.write(("minx=" + minx + ",miniy=" + miny + ".\n").getBytes());
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j); // 下面三行代碼將一個數字轉換為RGB數字
rgb[0] = (pixel & 0xff0000) >> 16;
rgb[1] = (pixel & 0xff00) >> 8;
rgb[2] = (pixel & 0xff);
System.out.println("i=" + i + ",j=" + j + ":(" + rgb[0] + ","+ rgb[1] + "," + rgb[2] + ")");
bos.write(("i=" + i + ",j=" + j + ":(" + rgb[0] + ","+ rgb[1] + "," + rgb[2] + ")\n").getBytes());
}
}
}
/**
* 返回屏幕色彩值
*
* @param x
* @param y
* @return
* @throws AWTException
*/
public int getScreenPixel(int x, int y) throws AWTException { // 函數返回值為顏色的RGB值。
Robot rb = null; // java.awt.image包中的類,可以用來抓取屏幕,即截屏。
rb = new Robot();
Toolkit tk = Toolkit.getDefaultToolkit(); // 獲取預設工具包
Dimension di = tk.getScreenSize(); // 屏幕尺寸規格
System.out.println(di.width);
System.out.println(di.height);
Rectangle rec = new Rectangle(0, 0, di.width, di.height);
BufferedImage bi = rb.createScreenCapture(rec);
int pixelColor = bi.getRGB(x, y);
return 16777216 + pixelColor; // pixelColor的值為負,經過實踐得出:加上顏色最大值就是實際顏色值。
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
int x = 0;
ReadColorTest rc = new ReadColorTest();
x = rc.getScreenPixel(100, 345);
System.out.println(x + " - ");
rc.getImagePixel("D:\\car.jpg");
}
}
⑺ java實現將結果輸出到txt
遍歷所有數組,然後用FileInputStream()
FileOutputStream fos = new FileOutputStream("d://data.txt",true);
⑻ Java中怎麼寫數據輸出為.txt格式的文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//簡單的一個例子,來模擬內輸容出
⑼ java中輸出txt文件
不清楚樓主具體是哪裡不懂,先給個大概思路,後台也就是action或servlet中利用java.io包把文本讀出來,然後setAttribute()到頁面上去,頁面上textarea取set進去的值就ok了
寫文件大同小異,流程相反
附上io的一個例子
final int BUFFER_LENGTH=1024;
public void formatFlie(String fileName) {
char[] c=new char[BUFFER_LENGTH];//buffered area
String inStr="";//read String
try {
BufferedReader readBuff=new BufferedReader(new FileReader(fileName));
while(readBuff.read(c,0,BUFFER_LENGTH)!=-1) {
//System.out.println("pos:"+read_pos);
for(int i=0;i<BUFFER_LENGTH;i++)
inStr+=c[i];
}
System.out.println(inStr);
readBuff.close();
}
catch(IOException e)
{
System.out.println(e.toString());
}
}
⑽ java 數據輸出到txt文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//簡單的一個例子,來模擬輸出