㈠ 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文件
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中将数据导出成XML文件(急急急急)
2./**
1. * desciption:java create xml file
2. * author:maomao
3. * datetime:2007/04/04 23:42
4. */
5.
6.package com.xh.xml;
1.
2.import java.io.FileOutputStream;
1.import java.io.IOException;
1.import org.jdom.Document;
1.import org.jdom.Element;
1.import org.jdom.JDOMException;
1.import org.jdom.output.XMLOutputter;
1.
2.public class Java2XML {
1.
2. public void BuildXMLDoc() throws IOException, JDOMException {
3.
4. // 创建根节点 list;
5. Element root = new Element("list");
6.
7. // 根节点添加到文档中;
8. Document Doc = new Document(root);
9.
10. // 此处 for 循环可替换成 遍历 数据库表的结果集操作;
11. for (int i = 0; i < 5; i++) {
12.
13. // 创建节点 user;
14. Element elements = new Element("user");
15.
16. // 给 user 节点添加属性 id;
17. elements.setAttribute("id", "" + i);
18.
19. // 给 user 节点添加子节点并赋值;
20. // new Element("name")中的 "name" 替换成表中相应字段,setText("xuehui")中 "xuehui 替换成表中记录值;
21. elements.addContent(new Element("name").setText("xuehui"));
22. elements.addContent(new Element("age").setText("28"));
23. elements.addContent(new Element("sex").setText("Male"));
24.
25. // 给父节点list添加user子节点;
26. root.addContent(elements);
27.
28. }
29. XMLOutputter XMLOut = new XMLOutputter();
30.
31. // 输出 user.xml 文件;
32. XMLOut.output(Doc, new FileOutputStream("user.xml"));
33. }
34.
35. public static void main(String[] args) {
36. try {
37. Java2XML j2x = new Java2XML();
38. System.out.println("生成 mxl 文件...");
39. j2x.BuildXMLDoc();
40. } catch (Exception e) {
41. e.printStackTrace();
42. }
43. }
44.
45.}
生成结果:
# <?xml version="1.0" encoding="UTF-8"?>
# <list>
# <user id="0">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="1">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="2">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="3">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# <user id="4">
# <name>xuehui</name>
# <age>28</age>
# <sex>Male</sex>
# </user>
# </list>
你可以参考一下链接,然后拷贝代码,这里我直接拷贝带上行号。
㈣ 用JAVA怎样才能将数据输出到文件的指定位置
参考这程序吧
/*
* c7:IODemo4.java 1.0 05/02/10
* author:Leo
* This program is protected by right laws.
* Reader from and write to files.
* 字符流和字节流类似,但效率更高.有可能的情况下,尽量使用reader和writer
* 一般只要把Reader替换为InputStream,用Writer 替换OutputStream,当然还要用Char替换Byte.
* 用reader和writer可以一次读一行,而且没有类型转换的麻烦 .
*/
package myprojects.readerwriter;
import java.io.*;
class IODemo4
{
public static void main(String args[])
{
System.out.println("Starting reader...");
String s = new String();
String s2=new String();
try
{
BufferedReader in = new BufferedReader(new FileReader("d:\\classtry\\My.class"));
PrintWriter out = new PrintWriter(new FileWriter("d:\\classtry\\destnation.xls"));
//BufferedReader in2 = new BufferedReader(new FileReader("d:\\javatry\\classtry\\destnation.xls"));
//把下面的字符串写入到文件的第一行.
//out.println("the following is from source");
//下面的循环语句读入一行内容的同时进行写入.
while((s=in.readLine()) != null)
{
System.out.println(s);
//char ss[]=s.toCharArray();
out.println(s+"\t");
}
//in2.close();
in.close();
out.close();//关闭文件.
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
㈤ java将查询数据导出成csv文件的问题
生成.csv文件有第三方包javacsv.jar,例子网上找,很简单
提示用户下载,用流实现,网络上更多
提供个JSP的例子给你。
<%@ page language="java" pageEncoding="UTF-8"%>
<%
// example:
// <a href="download.jsp?p=img/test.gif">download image</a>
String path = request.getParameter("p");
String name = request.getParameter("name");
String root = getServletContext().getRealPath(path);
if (name == null) {
int index = path.lastIndexOf("/");
if (index >= 0) {
name = path.substring(index + 1);
} else {
name = path;
}
}
response.setContentType("unknown");
response.addHeader("content-disposition", "filename=\"" + name + "\"");
java.io.OutputStream os = response.getOutputStream();
try {
java.io.FileInputStream fis = new java.io.FileInputStream(root);
byte[] b = new byte[1024];
int i = 0;
while ( (i = fis.read(b)) > 0 ) {
os.write(b, 0, i);
}
fis.close();
os.flush();
os.close();
}
catch ( Exception e )
{
e.printStackTrace();
}
out.clear();
out = pageContext.pushBody();
%>
㈥ java 上百万的数据量如何导出到excel文件
一个sheet最多6W多行;所以你应该1W行一个sheet。
也可以导出为csv文件,这个会比较小点
㈦ 在java中,如何将内存中的数据结构直接导出到一个文件中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;
public class test {
public static void main(String[] args) throws Exception {
Map map = new HashMap();
map.put("1", "test1");
map.put("2", "test2");
map.put("3", "test3");
map.put("4", "test4");
//写对象
FileOutputStream fos = new FileOutputStream(new File("test.txt"));//声明要写入的文件
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(map);
//读对象
FileInputStream fis = new FileInputStream(new File("test.txt"));
ObjectInputStream in = new ObjectInputStream(fis);
HashMap target = (HashMap)in.readObject();
Set set = target.keySet();
for(Object obj: set){
String key = (String)obj;
System.out.println(key+"-"+target.get(key));
}
}
}