導航:首頁 > 編程語言 > java導出txt文件

java導出txt文件

發布時間:2022-07-06 12:15:56

java如何將一個txt文件導出並顯示

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class IOTexst {

public static void main(String[] arg){
String temp=null;
BufferedReader br;
try {
br = new BufferedReader(new FileReader("d:/test.txt"));
while((temp=br.readLine())!=null){
System.out.println(temp);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

❷ java把運行結果輸出到txt

這個最主要的就是萬年歷演算法,網上一搜就有 輸出保存到新建的Txt文件很容易就是IO寫入操作例如:

BufferedWriterwriter=newBufferedWriter(newFileWriter(txt文件路徑加路徑名,
false));//true表示追加
writer.write(要寫入的數據);
writer.close();

❸ 如何用java輸出txt文件

輸入無需使用位元組流,直接字元流讀取即可。

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的方法有很多的。常用位位元組流和字元流
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class TextFileGenerator {
public static void main(String[] args) throws Exception {
method1();
method2();
}

private static void method1() throws Exception {

String txtContent = "Hello World!";

File file = new File("test1.txt");
FileWriter fw = new FileWriter(file);
fw.write(txtContent);
fw.close();

}

private static void method2() throws Exception {

String txtContent = "Hello World!";

File file = new File("test2.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(txtContent.getBytes());
fos.close();

}

}

❺ java輸出到TXT文件時怎麼加換行

java輸出到txt的時候增加換行符的方法如下:
package
com.anjoyo.test;
import
java.io.FileWriter;
import
java.io.IOException;
public
class
TestFileWriter
{
public
static
void
main(String[]
args)
throws
IOException{
//\r\n為換行符
FileWriter
fw
=
new
FileWriter("D:\\1.txt");
//寫入第一行換行
fw.write("第一行\r\n");
//或者獲得系統換行符
String
str
=
"第二行"
+
System.getProperty("line.separator");
fw.write(str);
fw.write("第三行");
fw.close();
/*
*
windows下的文本文件換行符:\r\n
linux/unix下的文本文件換行符:\r
*
Mac下的文本文件換行符:\n
*/
}
}

❻ java生成txt文件 急急急!!!

package file;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/** 用FileOutputStream類往指定文件中寫入數據 */
public class FileOutputStreamTest {
public static void main(String[] args) {
FileOutputStream out = null;
try {
//step1: 創建一個向指定名的文件中寫入數據的FileOutputStream
//第二個參數設置為true表示:使用追加模式添加位元組
out = new FileOutputStream("D:\\IOTest\\dest.txt",true);
//step2: 寫數據
out.write('#');
out.write("helloWorld".getBytes());
out.write("你好".getBytes());
out.write("\r\n".getBytes());//換行
out.write("網路新浪".getBytes());

//step3: 刷新此輸出流
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) { // 捕獲IO異常
e.printStackTrace();
}finally{
if(out != null){
try {
out.close(); //step4: 關閉輸出流
} catch (IOException e) {
e.printStackTrace();
} } } }}

給你看看我寫的 參考下吧

❼ 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文件

importjavax.swing.*;
importjavax.swing.filechooser.*;
importjavax.swing.text.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
/**
*格子繪圖演示
*@authorhardneedl
*/
{
=newDimension(600,400);

publicDimensiongetMinimumSize(){returnSIZE;}
publicDimensiongetMaximumSize(){returnSIZE;}
(){returnSIZE;}

publicStringgetTitle(){return"NotePadDemo";}

{
;
;
SaveAction(JTextComponentt){
super("保存...");
textComponent=t;
fileChooser=newJFileChooser(".");
fileChooser.setFileFilter(newFileNameExtensionFilter("文本文件","txt"));
}

publicvoidactionPerformed(ActionEvente){
if(fileChooser.showDialog(null,"保存")==JFileChooser.APPROVE_OPTION){
Filef=fileChooser.getSelectedFile();
try{
FileWriterfw=newFileWriter(f);
fw.write(textComponent.getText());
fw.close();
}catch(IOExceptione1){
e1.printStackTrace();
}
}
}
}

privateJTextAreatextArea;

privateNotePadDemo()throwsHeadlessException{
super();
init();
addListeners();
doLay();
}

privatevoidinit(){
textArea=newJTextArea();
}

privatevoiddoLay(){
JMenuBarmenuBar=newJMenuBar();
setJMenuBar(menuBar);

JMenufileMenu=newJMenu("文件");
fileMenu.add(newSaveAction(textArea));

menuBar.add(fileMenu);

getContentPane().add(newJScrollPane(textArea),BorderLayout.CENTER);
pack();
setVisible(true);
}

privatevoidaddListeners(){
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
super.windowClosing(e);
}
});
}

publicstaticvoidmain(String...args){
SwingUtilities.invokeLater(NotePadDemo::new);
}
}

❾ 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文件的問題

我覺的你的問題在於在循環中一直調用response.getWriter().print();這句,計算機運行中response.getWriter()會不停的生成一個PrintWriter類的對象,導致堆空間在短時間內生成大量的對象,在垃圾回收器未來的及回收之前就內存溢出了。
建議修改:在循環外使用PrintWriter pw=response.getWriter();
循環內使用pw.print();方法。再試試看

如果你的list里放了太多的數據,這樣自身就會內存溢出。list中的對象如果沒內存溢出,再使用上面說的方法試,不要再用StringBuffer存list中的數據,StringBuffer存list內數據時也是佔用內存的,這樣你內存消耗的更快。

list最好分成多次存儲對象。

閱讀全文

與java導出txt文件相關的資料

熱點內容
自己購買雲主伺服器推薦 瀏覽:419
個人所得稅java 瀏覽:760
多餘的伺服器滑道還有什麼用 瀏覽:189
pdf劈開合並 瀏覽:26
不能修改的pdf 瀏覽:750
同城公眾源碼 瀏覽:488
一個伺服器2個埠怎麼映射 瀏覽:297
java字元串ascii碼 瀏覽:78
台灣雲伺服器怎麼租伺服器 瀏覽:475
旅遊手機網站源碼 瀏覽:332
android關聯表 瀏覽:945
安卓導航無聲音怎麼維修 瀏覽:332
app怎麼裝視頻 瀏覽:430
安卓系統下的軟體怎麼移到桌面 瀏覽:96
windows拷貝到linux 瀏覽:772
mdr軟體解壓和別人不一樣 瀏覽:904
單片機串列通信有什麼好處 瀏覽:340
游戲開發程序員書籍 瀏覽:860
pdf中圖片修改 瀏覽:288
匯編編譯後 瀏覽:491