我正在编写一些使用FileReader从一些输入文件加载数据的Java代码.我正在大量使用TDD,并且我想添加一些测试,以确保在我完成它时通过在阅读器上调用close()来正确清理.不幸的是,我无法想出一个很好的方法来测试它.有人有任何见解吗?
编辑添加:我知道我可以使用模拟对象显式测试close调用,但我想尽可能避免使用它,部分原因是因为我发现它们导致了一些比较脆弱的代码,部分是因为我很好奇它是否可能编写能识别不关闭文件效果的代码.)
我试图从txt文件(书)中读取,然后将其每一行添加到链表.但是,当我运行代码时,我得到了一个outofmemory错误l.add(line);.你能告诉我这段代码我做错了什么吗?或者,有没有更好的方法来存储String值而不是LinkedList?
非常感谢!
public Book (String bookname) throws java.io.IOException{
f = new FileReader(bookname);
b = new BufferedReader(f);
l = new LinkedList<String>();
String line = b.readLine();
while (line != null) {
l.add(line);
}
b.close();
}
Run Code Online (Sandbox Code Playgroud) 我知道这是基本的但不知怎的,我已经被困在这里一段时间了.如果我必须从文本文件中读取一些这样的模式
10 9 20 12 14 29
11 3 16 30 33 26
12 20 10 14 8
Run Code Online (Sandbox Code Playgroud)
我希望所有这些数字都有所不同.什么是需要使用的基本java函数.我确实尝试使用修剪,然后拆分,但我没有得到正确的结果传递"|\t"作为拆分的参数.任何帮助,将不胜感激.
我无法在下面的代码中解决以下异常.我使用BufferedReader的方式有什么问题?我在main方法中使用BufferedReader
输出: -
ParseFileName.java:56: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
BufferedReader buffread = new BufferedReader (new FileReader("file.txt"));
Run Code Online (Sandbox Code Playgroud)
// ParseFileName is used to get the file name from a file path
// For eg: get - crc.v from "$ROOT/rtl/..path/crc.v"
import java.util.regex.Pattern;
import java.io.*;
public class ParseFileName {
//Split along /'s , and collect the last term.
public String getName (String longName) {
String splitAt = "/";
Pattern pattern1 = Pattern.compile(splitAt);
String[] parts = pattern1.split(longName);
System.out.println("\nparts.length = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的文本文件,由于某种原因,它无法找到.我没有看到代码有什么问题,因为我是从网站上得到的,我开始认为我没有将文本文件放在正确的位置.有什么建议吗?
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MainFavorites {
public static void main(String[] args) throws IOException {
/**
* finds pathway to the file
*/
// File file = new File("icecreamTopping.txt");
// System.out.println(file.getCanonicalPath());
BufferedReader reader = null;
ArrayList <String> myFileLines = new ArrayList <String>();
try {
String sCurrentLine;
reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
while ((sCurrentLine = reader.readLine()) != null) {
//System.out.println(sCurrentLine);
myFileLines.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace(); …Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的(对我来说至少),希望有人能够提供帮助.也许我只是在考虑这个错误的方式或者其他什么.尝试使用javascript中的文件对我来说是全新的.
我有一个指令,其模板如下所示:
<input type="file" ng-file-select ng-model="files">
<button ng-click="onFileSelect()">Upload</button>
Run Code Online (Sandbox Code Playgroud)
在指令的控制器中,我这样做:
$scope.onFileSelect = function() {
reader = new FileReader();
reader.onload = function() {
$scope.file_contents = this.result;
};
reader.readAsArrayBuffer($scope.files[0]);
//debugger;
$scope.$watch('file_contents',function(file_contents) {
console.log(file_contents);
if (angular.isDefined(file_contents)) {
... bunch of code attempting to deal with file_contents ...
}
});
};
Run Code Online (Sandbox Code Playgroud)
我选择一个文件,然后单击"上传"按钮.(file_contents)的console.log未定义.只有当我第二次点击按钮时,它才有值.
如果我取消注释调试器,$ scope.file_contents在我检查它时会有一个值.
因此,file_contents需要一些时间来设置(这是预期的),但$ watch似乎从未注意到?这有什么奇怪的原因吗?$ watch无法使用FileReader对象吗?
编辑1:
好的.这是一些更多的信息.根据PSL的建议,我现在有了这样的代码:
$scope.onFileSelect = function() {
reader = new FileReader();
reader.onload = function() {
file_contents = this.result;
upload_file($scope.files[0],file_contents);
};
reader.readAsArrayBuffer($scope.files[0]);
};
upload_file = function(file,file_contents) {
<lots of cool …Run Code Online (Sandbox Code Playgroud) 现在许多浏览器都支持使用HTML5的FileReader读取本地文件,这为超出"数据库前端"的网站打开了大门,这些脚本可以对本地数据执行一些有用的操作,而无需先将其发送到服务器.
在上传前预处理图像和视频,FileReader的一个大应用是将数据从某种磁盘表(CSV,TSV等)加载到浏览器中进行操作 - 可能用于D3.js中的绘图或分析或在WebGL中创建景观.
问题是,StackOverflow和其他站点上的大多数示例都使用FileReader的.readAsText()属性,该属性在返回结果之前将整个文件读入RAM.
要在不将数据加载到RAM的情况下读取文件,需要使用.readAsArrayBuffer(),这个SO帖子是最接近我能得到一个好答案的:
然而,这个特定问题有点过于具体,而且说实话,我可以尝试好几天让解决方案更加通用,然后空手而归,因为我不明白块大小的重要性或为什么Uint8Array是用过的.使用用户可定义的行分隔符逐行读取文件的更一般问题的解决方案(理想情况下使用.split(),因为它也接受正则表达式),然后按行执行某些操作(例如将其打印到console.log)是理想的.
如何上传500mb文件并使用CryptoJS获取MD5哈希?
这是我的代码:
$('#upload-file').change(function(){
var reader = new FileReader();
reader.addEventListener('load',function () {
var hash = CryptoJS.MD5(CryptoJS.enc.Latin1.parse(this.result));
window.md5 = hash.toString(CryptoJS.enc.Hex);
});
reader.readAsBinaryString(this.files[0]);
});
Run Code Online (Sandbox Code Playgroud)
如果文件低于200mb,它可以工作.任何更大的东西,this.result都是空的"".
我试过了:
javascript FileReader - 以块的形式解析长文件
几乎让这个工作,但控制台抱怨.join("")
http://dojo4.com/blog/processing-huge-files-with-an-html5-file-input
我想将一个单词替换为现有文本文件中的另一个单词.我认为我的代码是正确的,但每当我运行程序时,所有文本都会在文本文件中被删除.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String ar[]) throws IOException{
String getText, putText;
File f = new File("Test.txt");
FileReader fr ;
BufferedReader br ;
BufferedWriter bw;
if(f.exists()){
try{
fr = new FileReader(f);
br = new BufferedReader(fr);
bw = new BufferedWriter(new FileWriter(f));
while((getText = br.readLine()) != null){
System.out.println(getText);
if(getText != null){
putText = getText.replaceFirst("Dog", "Cat");
bw.write(putText);
}
}
bw.close();
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}else{ …Run Code Online (Sandbox Code Playgroud) 所以,我需要逐行读取文本文件,并按字符串返回它们.我可以指定从哪行到哪一行我想读它.
我的班有3种方法:
public class FilePartReader {
String filePath;
Integer fromLine;
Integer toLine;
public FilePartReader() { }
public void setup(String filepath, Integer fromLine, Integer toLine) {
if (fromLine < 0 || toLine <= fromLine) {
throw new IllegalArgumentException(
"fromline cant be smaller than 0 and toline cant be smaller than fromline");
}
this.filePath = filepath;
this.fromLine = fromLine;
this.toLine = toLine;
}
public String read() throws IOException {
String data;
data = new String(Files.readAllBytes(Paths.get(filePath)));
return data;
}
public String readLines() { …Run Code Online (Sandbox Code Playgroud) filereader ×10
java ×7
javascript ×3
file ×2
ioexception ×2
java-io ×2
angularjs ×1
cryptojs ×1
html5 ×1
junit ×1
linked-list ×1
md5 ×1
text-files ×1
unit-testing ×1