在我工作的地方,曾经有过每个文件超过百万行的文件.即使服务器内存超过10GB,8GB用于JVM,有时服务器会被暂停一段时间并扼杀其他任务.
我对代码进行了分析,发现虽然文件读取内存使用频繁增加千兆字节(1GB到3GB)然后突然恢复正常.似乎这种频繁的高内存和低内存使用会挂起我的服务器.当然这是由于垃圾收集.
我应该使用哪个API来读取文件以获得更好的性能?
现在我正在使用BufferedReader(new FileReader(...))读取这些CSV文件.
过程:我如何阅读文件?
更新
我这样读取30或31个文件(一个月的数据)并将符合条件存储在地图中.后来这张地图用于在不同的表格中获得一些罪魁祸首.因此必须读取并存储该数据.虽然我现在已经将HashMap部分切换到BerkeleyDB但是在读取文件时的问题是相同甚至更糟.
真的是新手问题:
我有一个我需要阅读的.csv文件.我把它放在原始文件夹中.为方便起见,我使用http://opencsv.sourceforge.net/库来读取文件.该库提供了此方法来创建CSVReader对象:
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何将这个构造函数指向我的文件,因为Android中的文件通常被引用为R.raw.file而不是文件的String地址.
任何帮助将不胜感激.
在Snow Leopard的Eclipse(Indigo)上运行以下Java 6代码时,出现FileNotFoundException异常:
import java.io.*;
import java.util.*;
public class readFile {
public static void main(String[] args) {
Scanner s = new Scanner(new FileReader("/Users/daniel/pr/java/readFile/myfile.txt")); // Line 9
}
}
Run Code Online (Sandbox Code Playgroud)
例外是
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at readFile.main(readFile.java:9)
Run Code Online (Sandbox Code Playgroud)
我当前的工作空间是/ Users / daniel / pr / java。它仅包含一个项目(readFile),文件层次结构如下所示:
- readFile
- src
- (default package)
- readFile.java
- JRE System Library [JavaSE-1.6]
- myfile.txt
Run Code Online (Sandbox Code Playgroud)
在阅读了几个非常相似的问题之后,我尝试了
我很茫然。可能是什么问题呢?(谢谢您的阅读!)
java eclipse filenotfoundexception filereader java.util.scanner
请考虑以下代码
function readSingleFile(evt) {
//Retrieve the first (and only!) File from the FileList object
var myFile = evt.target.files[0];
var reader = new FileReader();
reader.readAsText(myFile);
var myString = reader.toString();
alert(myString); // print - "[object FileReader]"
}
Run Code Online (Sandbox Code Playgroud)
我尝试将所有文件内容放入String,例如,如果文件内容是
helloWorld1
helloWorld2
Run Code Online (Sandbox Code Playgroud)
我会得到alert那个内容.
我正在尝试将文件上传到我的服务器.但我不确定如何使用readAsArrayBuffer来做到这一点.如果我使用readAsBinaryString,这是有效的.
如果我尝试console.log它只返回'arrayBuffer:{}'. 在我尝试上传之后,我可以看到内部帖子中只发送了一个空对象.如果我使用readAsBinaryString,我会看到一堆二进制代码.
var file = document.getElementById('my_file').files[0],
reader = new FileReader();
reader.onloadend = function(e){
console.log(e.target.result);
$scope.image = e.target.result;
}
reader.readAsArrayBuffer(file);
Run Code Online (Sandbox Code Playgroud)
我怎么能看到我的文件,所以我知道它在使用readAsArrayBuffer时有效?如果需要更多代码,请告诉我!谢谢.
我正在使用ngCordova Capture通过录制音频来编写此代码,并将base64发送到某处(通过REST).我可以让Capture Audio工作但是一旦它返回audioURI,我就无法将文件系统中的数据作为base64.我的代码如下:
$cordovaCapture.captureAudio(options).then(function(audioURI) {
$scope.post.tracId = $scope.tracId;
$scope.post.type = 'audio';
console.log('audioURI:');
console.log(audioURI);
var path = audioURI[0].localURL;
console.log('path:');
console.log(path);
window.resolveLocalFileSystemURL(path, function(fileObj) {
var reader = new FileReader();
console.log('fileObj:');
console.log(fileObj);
reader.onloadend = function (event) {
console.log('reader.result:');
console.log(reader.result);
console.log('event.result:');
console.log(event.result);
}
reader.onload = function(event2) {
console.log('event2.result:');
console.log(event2.target.result);
};
reader.readAsDataURL(fileObj);
console.log(fileObj.filesystem.root.nativeURL + ' ' + fileObj.name);
$cordovaFile.readAsDataURL(fileObj.filesystem.root.nativeURL, fileObj.name)
.then(function (success) {
console.log('success:');
console.log(success);
}, function (error) {
// error
});
});
Run Code Online (Sandbox Code Playgroud)
这是控制台日志中的输出:

那么如何从.wav文件中获取base64数据呢?
我一直在阅读这些链接:
PhoneGap FileReader/readAsDataURL不触发回调
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
http://jsfiddle.net/eliseosoto/JHQnk/
http://community.phonegap.com/nitobi/topics/filereader_onload_not_working_with_phonegap_build_2_5_0
我正在尝试在我的项目中实现javascript的FileReader接口。
要求:有一个启用了“多个”的文件输入字段,我希望用户应该选择一些图像,甚至在单击提交之前,也应该在页面上看到这些图像,这样,如果(s )他要!)。但是问题在于屏幕上仅显示一张图像。标签已创建,但src为空,除了最后一个图像。
代码:
<html>
<head>
<meta charset="windows-1252">
<title></title>
</head>
<body>
<div>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="abc[]" multiple/>
<input type="submit"/>
</form>
</div>
<img id="image0"/>
</body>
<script src="resources/js/jquery-2.1.3.min.js" type="text/javascript"></script>
<script>
$('document').ready(function () {
$("input[name='abc[]']").change(function (e) {
console.log(e.originalEvent.srcElement.files.length);
for (var i = 1; i <= e.originalEvent.srcElement.files.length; i++) {
var file = e.originalEvent.srcElement.files[i-1];
var img = document.createElement("img");
img.id = "image"+i;
var reader = new FileReader();
reader.onloadend = function () {
img.src = reader.result;
}
reader.readAsDataURL(file);
$("#image"+(i-1)).after(img);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
例如,如果我选择了img1,则在图像选择弹出窗口中。img2和img3,仅显示img3。但是,在点击提交时,我确实在服务器端获得了所有图像: …
我正在尝试读取所选文件的第一个字节。
但是,当我选择一个大文件(> 100Mb)时,会出现错误:“ NotReadableError”。
请参见下面的代码。“数组缓冲区”真的是缓冲区吗,还是只是将所有内容加载到内存中,而我必须使用file#slice?
function readFile(file) {
var reader = new FileReader();
reader.onload = function() {
var buffer = reader.result;
var view = new Int8Array(buffer);
try {
view.forEach(function(v, index, array) {
console.log(v);
alert("ok - " + v);
throw "BreakException";
})
} catch (e) {
if (e!=="BreakException") throw e;
}
}
reader.onerror = function() {
alert("error");
console.log(reader.error);
}
reader.readAsArrayBuffer(file);
}
var fileField = document.getElementById("file");
fileField.onchange = function(e) {
var file = e.target.files[0];
readFile(file);
}Run Code Online (Sandbox Code Playgroud)
<form>
<input id="file" type="file"/> …Run Code Online (Sandbox Code Playgroud)嗨,我使用原生HTML5拖放将文件上传到服务器.我想获取从本地系统上传的文件的名称.我无法获取放置区域中丢弃的文件的名称.以下是我尝试过的代码.任何帮助,将不胜感激
var reader = new FileReader();
reader.onload = function (event) {
var childrenDivs = document.getElementById("holder").children
var temp
if(childrenDivs.length > 0){
var lastDiv = childrenDivs[childrenDivs.length - 1]
temp = parseInt(lastDiv.id.split("_")[1]) + 1
}else{
temp = 1
}
var imageDiv = document.createElement('div')
imageDiv.id = "uploadedImage_"+temp
var image = new Image();
image.src = event.target.result;
console.log(event.target.fileName)
Run Code Online (Sandbox Code Playgroud)
而不是console.log(event.target.fileName)我自己也尝试console.log(event.target.files[0].name)和console.log(event.dataTrasfer.files[0]).有人可以帮帮我吗.
我试图创建Angular 2/4服务,可能上传文件.我找不到任何资源的解决方案,所以我可能想问你们.所以这个想法在Component的某个地方有输入字段,其中type = file.它有指令(change)="uploadFile($ event)".在组件.ts文件中:
uploadFile(event) {
this.images.push(this.uploadImgService.uploadImage(event));
}
Run Code Online (Sandbox Code Playgroud)
UploadImgService看起来像这样:
private img: string;
uploadImage(e) {
const file = e.target.files[0];
const pattern = /image-*/;
if (!file.type.match(pattern)) {
alert('You are trying to upload not Image. Please choose image.');
return;
}
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
this.img = reader.result;
};
return this.img;
}
Run Code Online (Sandbox Code Playgroud)
所以,我知道操作是异步的,但我无法弄清楚如何将它包装起来,直到img加载为止.我认为这是技能缺乏的结果:(当我将这些代码发布到组件中它肯定有效,但我的想法是提供服务.而且,我只是Angular的初学者.所以,如果有更好的方法重新定义这个想法我很高兴收到你的来信.谢谢!
filereader ×10
javascript ×7
java ×3
jquery ×2
android ×1
angular ×1
api ×1
audio ×1
berkeley-db ×1
cordova ×1
eclipse ×1
html ×1
html5 ×1
image ×1
ios ×1
performance ×1