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 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文件里,或者word文檔里!
給你一點關鍵部分的提示
你可能需要用到apache zip這個組件
// zipFileName為要解壓縮的zip為文件名,例:c:\\filename.zip
// outputDirectoty為解壓縮後文件名,例:c:\\filename
public void unZip(String zipFileName, String outputDirectory)
throws Exception {
InputStream in = null;
FileOutputStream out = null;
try {
zipFile = new ZipFile(zipFileName);
java.util.Enumeration e = zipFile.getEntries();
// org.apache.tools.zip.ZipEntry zipEntry = null;
createDirectory(outputDirectory, "");
while (e.hasMoreElements()) {
zipEntry = (ZipEntry) e.nextElement();
System.out.println("unziping " + zipEntry.getName());
if (zipEntry.isDirectory()) {
String name = zipEntry.getName();
name = name.substring(0, name.length() - 1);
File f = new File(outputDirectory + File.separator + name);
f.mkdir();
System.out.println("創建目錄:" + outputDirectory
+ File.separator + name);
} else {
String fileName = zipEntry.getName();
fileName = fileName.replace('\\', '/');
// System.out.println("測試文件1:" +fileName);
if (fileName.indexOf("/") != -1) {
createDirectory(outputDirectory, fileName.substring(0,
fileName.lastIndexOf("/")));
fileName = fileName.substring(
fileName.lastIndexOf("/") + 1, fileName
.length());
}
try {
f = new File(outputDirectory + File.separator
+ zipEntry.getName());
in = zipFile.getInputStream(zipEntry);
out = new FileOutputStream(f);
byte[] by = new byte[100000];
int c;
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
out.flush();
} catch (Exception ee) {
} finally {
if (in != null) {
in.close(); //解壓完成後注意關閉輸入流對象
}
if (out != null) {
out.close(); //解壓完成後注意關閉輸出流對象
}
}
}
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
zipFile.close(); //解壓完成後注意關閉apache自帶zip流對象
}
}
❹ 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文件,如何實現換行
1 實現的方式總共有3中, 1)原始的 \r\n 方式
2)BufferedWriter的newline()方法
3) System.getProperty()方法
下面使用代碼逐一展示 , 寫到.txt文件實現換行
2 使用java中的轉義符"\r\n":
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str="aaa";
str+="\r\n";
fos.write(str);
3 BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline(); //實現換行
4 System.getProperty()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
String str = "aaa"+System.getProperty("line.separator");
fos.write(str);
❻ JAVA中 如何把字元串輸入到一個TXT文件里,並從TXT裡面讀取出來輸出到控制台
import java.io.*;
public class FileWrite {
public static void main(String args[]){
BufferedReader br=null;
BufferedWriter bw=null;
BufferedReader br1=null;
try {
br=new BufferedReader(new InputStreamReader(System.in));//字元輸入流 從鍵盤讀取數據
bw=new BufferedWriter(new FileWriter("D:/temp/title.txt"));//字元輸出流 寫數據到文件
br1=new BufferedReader(new FileReader("D:/temp/title.txt"));//字元輸入流 從文件讀取數據
String str=br.readLine();
bw.write(str);
bw.flush();
System.out.println(br1.readLine());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
br.close();
bw.close();
br1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
❼ 怎麼用java實現在一個txt文件中根據關鍵字查找信息並輸出
個人愚見,還望指教
1、把txt文件封裝成file對象;
2、如果是純文字可以用FileReader如果不是的話可以用流轉換一下FileInputStream;
3、維護一個數組將內容讀取;
4、判斷數組裡面的關鍵詞;(可以吧數組轉換為String用contains方法)
❽ 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文件
輸入無需使用位元組流,直接字元流讀取即可。
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();
}
}