我使用getResourceAsStream来访问本地文件.它假设文件是什么编码?
我有一个InputSteam并且需要简单地String用完整的内容得到一个简单的.
这是如何在Java中完成的?
我正在编写一个小程序,它创建了我目录中所有文件的索引.它基本上遍历磁盘上的每个文件并将其存储到可搜索的数据库中,就像Unix的locate一样.问题是,由于我有大约一百万个文件,因此索引生成非常慢.
生成索引后,是否可以快速找到自上次运行以来在磁盘上添加或删除的文件?
编辑:我不想监视文件系统事件.我认为风险太高而无法实现同步,我更喜欢快速重新扫描,以便快速找到添加/删除文件的位置.也许目录上次修改日期或其他什么?
我刚做了一点基准.运行
dir /b /s M:\tests\ >c:\out.txt
Run Code Online (Sandbox Code Playgroud)
需要0.9秒,并提供我需要的所有信息.当我使用Java实现(很像这样)时,大约需要4.5秒.任何想法如何改善至少这种蛮力的方法?
相关文章:如何查看目录的子文件是否已更改
可能重复:
如何从文件内容创建Java String
我有一个html文件,我想用它来提取信息.为此,我正在使用Jsoup.现在使用Jsoup,我需要将html文件转换为字符串.我怎样才能做到这一点?
File myhtml = new File("D:\\path\\report.html")';
Run Code Online (Sandbox Code Playgroud)
现在,我想要一个包含html文件内容的String对象.
我有这个代码,用于确定单词(忽略大小写)是否包含在wordList文本文件中.但是,wordList文本文件可能有65000 ++行,并且只使用我的实现在下面搜索单词需要将近一分钟.你能想到更好的实施吗?
谢谢!
import java.io.*;
import java.util.*;
public class WordSearch
{
LinkedList<String> lxx;
FileReader fxx;
BufferedReader bxx;
public WordSearch(String wordlist)
throws IOException
{
fxx = new FileReader(wordlist);
bxx = new BufferedReader(fxx);
lxx = new LinkedList<String>();
String word;
while ( (word = bxx.readLine()) != null)
{
lxx.add(word);
}
bxx.close();
}
public boolean inTheList (String theWord)
{
for(int i =0 ; i < lxx.size(); i++)
{
if (theWord.compareToIgnoreCase(lxx.get(i)) == 0)
{
return true;
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我试图找出为什么这个特定的代码片段对我不起作用.我有一个applet,应该读取.pdf并用pdf-renderer库显示它,但出于某种原因,当我读入位于我服务器上的.pdf文件时,它们最终会被破坏.我已经通过再次写出文件来测试它.
我尝试在IE和Firefox中查看applet,并且发生了损坏的文件.有趣的是,当我尝试在Safari(对于Windows)中查看applet时,该文件实际上很好!我理解JVM可能会有所不同,但我仍然迷失方向.我已经用Java 1.5编译了.JVM是1.6.读取文件的代码段如下.
public static ByteBuffer getAsByteArray(URL url) throws IOException {
ByteArrayOutputStream tmpOut = new ByteArrayOutputStream();
URLConnection connection = url.openConnection();
int contentLength = connection.getContentLength();
InputStream in = url.openStream();
byte[] buf = new byte[512];
int len;
while (true) {
len = in.read(buf);
if (len == -1) {
break;
}
tmpOut.write(buf, 0, len);
}
tmpOut.close();
ByteBuffer bb = ByteBuffer.wrap(tmpOut.toByteArray(), 0,
tmpOut.size());
//Lines below used to test if file is corrupt
//FileOutputStream fos = new FileOutputStream("C:\\abc.pdf");
//fos.write(tmpOut.toByteArray());
return bb;
}
Run Code Online (Sandbox Code Playgroud)
我一定是在遗漏一些东西,而且我一直在试图解决这个问题.任何帮助是极大的赞赏.谢谢.
编辑: 为了进一步说明我的情况,我在阅读之后使用片段和之后的文件中的差异是,我在阅读后输出的内容明显小于原来的内容.打开它们时,它们不会被识别为.pdf文件.没有任何例外被抛出我忽略,我试着冲洗无济于事. …
如何获取文本文件的内容,同时保留文件末尾是否有换行符?使用这种技术,无法判断文件是否以换行符结尾:
BufferedReader reader = new BufferedReader(new FileReader(fromFile));
StringBuilder contents = new StringBuilder();
String line = null;
while ((line=reader.readLine()) != null) {
contents.append(line);
contents.append("\n");
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Spring JPA 访问我的数据库,但数据库的用户名和密码以两个文件的形式提供给我(username.txt 和password.txt)。安全准则是我们不应该在配置文件中粘贴密码或任何形式的密码(加密)。
我正在尝试找出方法来做这样的事情:
spring.datasource:
url: <db url>
username: "file:/path/username.txt"
password: "file:/path/password.txt"
Run Code Online (Sandbox Code Playgroud)
有谁知道如何实现与此非常相似的东西?
附言。我想在此 spring 属性文件中保留特定于环境的凭据文件路径。有多种方法可以将文件内容作为环境变量 java -Dusername=${cat /path/username} 传递,但我想避免将我的配置放在 spring 属性文件之外。
编辑:用户名和密码以明文形式存储在单独的文件中。
我们正在从Web服务流式传输CSV文件.看来我们在流式传输时丢失了新行字符 - 客户端将文件全部放在一行上.知道我们做错了什么吗?
码:
public static void writeFile(OutputStream out, File file) throws IOException {
BufferedReader input = new BufferedReader(new FileReader(file)); //File input stream
String line;
while ((line = input.readLine()) != null) { //Read file
out.write(line.getBytes()); //Write to output stream
out.flush();
}
input.close();
}
Run Code Online (Sandbox Code Playgroud) java ×9
file ×2
file-io ×2
applet ×1
filesystems ×1
pdf ×1
spring ×1
spring-boot ×1
stream ×1
streaming ×1
web-services ×1