我正在用Java编写一个程序,要求我比较2个文件中的数据.我必须检查文件1中的每一行与文件2的每一行,如果我找到匹配,则将它们写入第三个文件.在我读到文件2的末尾之后,如何将指针重置为文件的开头?
public class FiFo {
public static void main(String[] args)
{
FileReader file1=new FileReader("d:\\testfiles\\FILE1.txt");
FileReader file2=new FileReader("d:\\testfiles\\FILE2.txt");
try{
String s1,s2;
while((s1=file1.data.readLine())!=null){
System.out.println("s1: "+s1);
while((s2=file2.data.readLine())!=null){
System.out.println("s2: "+s2);
}
}
file1.closeFile();
file2.closeFile();
}catch (IOException e) {
e.printStackTrace();
}
}
}
class FileReader {
BufferedReader data;
DataInputStream in;
public FileReader(String fileName)
{
try{
FileInputStream fstream = new FileInputStream(fileName);
data = new BufferedReader(new InputStreamReader(fstream));
}
catch (IOException e) {
e.printStackTrace();
}
}
public void closeFile()
{
try{
in.close();
}
catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我知道FileReader对象在Safari 5.0.5中不可用.我有一个使用它的脚本,并认为我只能检测对象是否存在以运行一些备用代码,如此处所示,
http://www.quirksmode.org/js/support.html
所以我的代码是,
if( FileReader )
{
//do this
}else{
//the browser doesn't support the FileReader Object, so do this
}
Run Code Online (Sandbox Code Playgroud)
问题是,我在Safari中测试过,一旦它命中if语句,我就会收到此错误并且脚本停止运行.
ReferenceError:找不到变量:FileReader
那显然那不是处理它的最好方法吗?知道为什么这不起作用吗?
问题解决了,看了评论
我在HTML5文件API中遇到的第三个问题:我仍在Mac OS X Snow Leopard上使用Chrome 12,我仍在尝试使用HTML5文件API读取文件,但因为"SECURITY_ERR"而调用了FileHandler.error() occurres.我尝试读取的文件是来自桌面的常规.txt文件,但它既不能用于其他文件,尽管我可以使用常规应用程序打开它们.
function FileHandler(files, action) {
console.log('FileHandler called.');
this.files = files;
this.reader = new FileReader();
this.action = action;
this.handle = function() {
console.log('FileHandler.handle called.');
for (var i = 0; i < this.files.length; i++) {
this.reader.readAsDataURL(files[i]);
}
}
this.upload = function() {
console.log('FileHandler.upload called.');
console.log(this.reader);
data = {
content: this.reader.result
}
console.log(data);
}
this.error = function() {
console.log('An error occurred while reading the file.');
console.log(this.reader.error);
}
this.reader.onload = this.upload.bind(this);
this.reader.onerror = this.error.bind(this);
}
Run Code Online (Sandbox Code Playgroud)
该代码生成以下控制台输出:http …
我有一个文件输入:
<img id="uploadPreview">
<div id="changeImage">Change</div>
<div id="customImage">
<input type="file" id="myfile" multiple style="display:none" onchange="PreviewImage();" />
<div class='upload blank' id="add-image"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
功能如下:
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("myfile").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
function PreviewImage() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("myfile").files[0]);
$("#uploadPreview").removeClass('hide'); //for manipulating something in the dom
$('#changeImage').removeClass('hide'); //for manipulating something in the dom
$("#customImage").addClass('hide'); //these are for manipulating something in the dom
oFReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
};
Run Code Online (Sandbox Code Playgroud)
一切都很完美.现在我有一个更改按钮.我希望如果有人点击它,那么之前上传的文件细节就会消失.功能如下所示:
$('#changeImage').click(function(){
$('#uploadPreview').addClass('hide'); …Run Code Online (Sandbox Code Playgroud) 任何人都可以解释我的类之间的区别BufferedReader,FileReader以及Scanner?当我想阅读文本文件时使用哪一个?
我正在尝试使用FileReader和JSZip.js在浏览器中打开zip文件,然后处理其中包含的文件.我无法弄清楚如何正确地将FileReader对象传递给JSZip.
这是我用来加载javascript的页面的剥离版本:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="zipscan.js"></script>
<script type="text/javascript" src="jszip.js"></script>
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
</head>
<body>
<div id="openFile"><input type="file" id="inputFile" /></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
删除所有不必要的代码后,zipscan.js中的javascript:
function checkFiles()
{
//Check support for the File API support
if ( window.File && window.FileReader && window.FileList && window.Blob )
{
var fileSelected = document.getElementById( "inputFile" );
fileSelected.addEventListener( "change", handleFile, false );
}
else
{
alert( "Files are not supported" );
}
}
function handleFile( evt )
{
//Set wanted file object …Run Code Online (Sandbox Code Playgroud) 有没有一种方法可以在 JavaScript 中本地处理非常大的文件(例如 2GB 及以上)而不会使浏览器崩溃?
*我知道输入标签和 FileReader API,但似乎没有像 Node.js 流那样的东西。
我正在尝试使用 JavaScript 中的 FileApi 访问文本文件的前几行。
为此,我从文件的开头切片任意数量的字节并将 blob 交给 FileReader。
对于大文件,这需要很长时间,但据我所知,只需要访问文件的前几个字节。后台是否有一些实现需要在切片之前访问整个文件?它是否取决于 FileApi 的浏览器实现?我目前在 Chrome 和 Edge(铬)中进行了测试。
使用性能开发工具在 Chrome 中进行分析显示 reader.onloadend 之前有大量空闲时间,并且 ram 使用量没有增加。然而,这可能是因为 FileApi 是在浏览器本身中实现的,并没有反映在 JavaScript 性能统计中。
我的 FileReader 实现看起来像这样:
const reader = new FileReader();
reader.onloadend = (evt) => {
if (evt.target.readyState == FileReader.DONE) {
console.log(evt.target.result.toString());
}
};
// Slice first 10240 bytes of the file
var blob = files.item(0).slice(0, 1024 * 10);
// Start reading the sliced blob
reader.readAsBinaryString(blob);
Run Code Online (Sandbox Code Playgroud)
它工作正常,但如所描述的那样,对于大文件来说表现不佳。我尝试了 10kb、100mb 和 6gb。记录前 10kb 之前的时间似乎与文件大小直接相关。
您对如何提高读取文件开头的性能有什么建议吗?
编辑:使用@BenjaminGruenbaum 建议的响应和 DOM 流确实不能提高读取性能。 …
我正在构建Chrome打包应用程序.如果配置文件在资源目录中并且在启动时想要通过Javascript读取它,我想放置脚本配置.
例如
在这里,我希望main.js在开头加载config.properties文件并获取键值对.
有没有人做过这样的事情?
javascript jquery filereader properties-file google-chrome-app
想要将用户输入的文件作为文本进行简单读取.
可以依赖于现代浏览器的使用,所以我使用FileReader(它就像一个魅力).
reader.readAsText(myfile, encoding);
Run Code Online (Sandbox Code Playgroud)
我知道encoding默认为UTF-8.
但是,由于我的用户将从各种来源(Windows,Mac,Linux)和各种浏览器上传文件,我要求用户通过选择框提供编码.
因此,例如对于西欧的Windows文本文件,我希望用户选择例如windows-1252.
我无法找到FileReader支持的编码列表(假设这至少取决于浏览器).
我不是要求自动确定编码,我只是想以这样的方式填充我的选择框:
<select id="encoding">
<option value="windows-1252">Windows (Western Latin)</option>
<option value="utf-8">UTF-8</option>
<option value="...">...</option>
</select>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
我不想使用任何主要的第三方库.
奖金问题:
有没有一种简单的方法来获得有意义的价值翻译,例如cp10029意味着Mac(中欧)?