高手请进

ch_984326013 2011-09-28 04:19:21
怎么通过Java获取本地图片,这里不能用数据库!方法越多越好,最好能给个实例!呵呵
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ylovep 2011-09-28
  • 打赏
  • 举报
回复
把文件放在工程的目录下面就可以用相对路径了。
康派尔 2011-09-28
  • 打赏
  • 举报
回复
这是我给你的一个例子,这个例子测试通过,它可以读取以个TXT文件并转化为一个二维数组,你把代码改一下就可以用。

private double[][] loadTextFile(String filepath) throws IOException {
InputStream stream = new FileInputStream(filepath);
InputStreamReader reader = new InputStreamReader(stream);
BufferedReader breader = new BufferedReader(reader);
double[][] result = new double[getFileRows(filepath)][2];
String line = breader.readLine();
String[] mid = new String[] {};
int index = 0;
while (line != null) {
if (line.startsWith("X")) {
line = breader.readLine();
} else {
mid = line.split(",");
result[index][0] = Double.parseDouble(mid[0]);
result[index][1] = Double.parseDouble(mid[1]);
index++;
line = breader.readLine();
}

}
return result;

}

private int getFileRows(String filepath) throws IOException {
int count = 0;
File file = new File(filepath);
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String line = reader.readLine();
while (line != null) {
if (!line.startsWith("X"))
count++;
line = reader.readLine();
}
reader.close();

return count;
}

public enum FileType {
text, xls
}
ch_984326013 2011-09-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 ch_984326013 的回复:]
怎么通过Java获取本地图片,这里不能用数据库!方法越多越好,最好能给个实例!呵呵
[/Quote]
能具体点么?用file通过路径获取?但是能不能不同过绝对路径获取啊!
scrack 2011-09-28
  • 打赏
  • 举报
回复
文件操作
cyl713 2011-09-28
  • 打赏
  • 举报
回复
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class Test {
final String pic_path = "D:/aaaa.jpg";
final String out_path = "C:/123.jpg";

public static void main(String[] args) {
new Test().getPic();
}

public void getPic() {
File file = new File(pic_path);
try {
byte[] b = new byte[1024];
FileInputStream finput = new FileInputStream(file);
FileOutputStream fout = new FileOutputStream(out_path);

int k;
while ((k = finput.read(b)) != -1) {
fout.write(b, 0, k);
}

} catch (Exception e) {
e.printStackTrace();
}
}

}
goodtime 2011-09-28
  • 打赏
  • 举报
回复
通过文件系统呗

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧