我想知道如何逐字节读取文件,然后每n个字节执行一些操作。
例如:
说我有一个文件size = 50 bytes,我想将其分成几个块n bytes。然后,将每个块发送给函数,以对这些字节执行某些操作。块将在读取过程中创建,并在块达到n字节时发送到函数,这样我就不会使用太多内存来存储所有块。
我希望将函数的输出写入/附加在新文件上。
这是我读过的书,但我不知道它是对的:
fc = new JFileChooser();
File f = fc.getSelectedFile();
FileInputStream in = new FileInputStream(f);
byte[] b = new byte[16];
in.read(b);
Run Code Online (Sandbox Code Playgroud)
对于写过程,我还没有做任何事情。
请告诉我最佳/快速的方法:
1)将非常小的二进制文件加载到内存中.例如图标;
2)加载/读取大小为512Mb +的非常大的二进制文件.也许我必须使用内存映射IO?
3)当你不想考虑大小/速度但是必须做的事情时你的共同选择:将所有字节读入内存?
谢谢!!!
PS对不起,也许是微不足道的问题.请不要关闭它;)
PS2.C#的模拟问题镜像 ;
我正在尝试使用Java使用新数据更新文件.假设我有一个txt文件,我保存了以下数据:
id grade
3498 8
2345 9
5444 7
2222 5
Run Code Online (Sandbox Code Playgroud)
所以我试图根据用户键入的id更新成绩,但新的(更新的)文件具有以下类型:
id grade3498 62345 95444 72222 5
Run Code Online (Sandbox Code Playgroud)
等等....
我找不到这个不起作用的原因,我猜想它与重写数据时不添加新行有关,但即使我在outobj.write中添加新行字符("\n") (fileContent.toString()); 没有什么变化.
这是我的代码片段:
public String check(int num) throws RemoteException
{
String textinLine;
String texttoEdit;
File file=new File ("c:\\students.txt");
FileInputStream stream = null;
DataInputStream in =null;
BufferedReader br = null;
try
{
stream = new FileInputStream(file);
in =new DataInputStream(stream);
br = new BufferedReader(new InputStreamReader(in));
StringBuilder fileContent = new StringBuilder();
if ((num>0) && (num<6001))
{
while ((textinLine=br.readLine())!=null)
{
texttoEdit=Integer.toString(num);
System.out.println(textinLine);
String[] parts …Run Code Online (Sandbox Code Playgroud) 我在java中创建一个程序,它将读取一个文件并将每个单词放入一个数组中,这样我就可以在将每个单词排序为默认数组之后制作一个单词.我很清楚如何做到这一点,除了我的.txt文件没有被读取.我在src中有一个名为"input.txt"的文件,其中包含我正在编写的"anagram.java"程序,但是当代码为文件条目提供时,在输入文件名"input.txt"时,我的代码说文件不存在我得到这个:
Enter file name:
input.txt
Exception in thread "main" java.io.FileNotFoundException: input.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at anagram.main(anagram.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
Run Code Online (Sandbox Code Playgroud)
这是乱七八糟的行的代码:
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name: ");
String fileName = br.readLine();
File file = new File(fileName);
if(file.length() == 0)
{
System.out.println("File is empty");
System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)
显然输入"input.txt"的信息还不够,我不确定.我删除了
if(file.length() == 0)
{
System.out.println("File is …Run Code Online (Sandbox Code Playgroud) 从昨天开始,我正在阅读Java在线文档,我注意到它更多地关注实践,而不是描述该库的机制.
由于IO包中有很多类,我如何理解何时使用以及如何使用它们?我对它的模型比对它的内容更感兴趣.
欢迎提供建议和手册.
先感谢您...
我试图从Internet下载XML文件,然后将此文件放在目录(C:/ Nationstates)中.
File theDir = new File("/NationStates");
if (!theDir.exists()) {
System.out.println("creating directory: /NationStates" );
boolean result = false;
try{
theDir.mkdir();
result = true;
} catch(SecurityException se){
System.out.println("Dir exists");
}
}
new PrintWriter("/NationStates/NS.xml");
URL website = new URL("https://www.nationstates.net/cgi-bin/api.cgi?nation=ageena");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("/NS.xml");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
Run Code Online (Sandbox Code Playgroud)
我可以创建目录,并创建文件(NS.XML)但是当我尝试写入文件时,我收到以下错误:
Exception in thread "main" java.io.FileNotFoundException: \NS.xml (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at XML.main(XML.java:31)
Run Code Online (Sandbox Code Playgroud)
它表示访问被拒绝,但我刚刚在同一位置的该目录中创建了一个目录和一个文件.
任何想法如何解决这一问题?
我想算一下行号.使用Java LineNumberReader的文件.输出有问题.问题是替代线显示为行号.1,3,5,...并且在计算总数没有线路时 我得到了一半没有.总实际行数.这是代码
import java.lang.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
public class countLine{
File file=null;
public countLine(){
file =new File("E:\\test.txt");
getFileData();
}
public void getFileData(){
try{
if(file.exists()){
FileReader fr = new FileReader(file);
LineNumberReader lnr = new LineNumberReader(fr);
int linenumber = 0;
do{
System.out.println(lnr.readLine());
linenumber++;
}while (lnr.readLine() != null);
System.out.println("Total number of lines : " + linenumber);
lnr.close();
}else{
System.out.println("File does not exists!");
}
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String …Run Code Online (Sandbox Code Playgroud) 我正在阅读关于java中的网络(客户端/服务器)的本教程,我对我不理解的东西有疑问.
https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
我经常看到这个
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
Run Code Online (Sandbox Code Playgroud)
和
BufferedReader inputStream = new BufferedReader(new FileReader("xanadu.txt"));
PrintWriter outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));
Run Code Online (Sandbox Code Playgroud)
我知道BufferReader的构造函数是
BufferedReader(Reader in)
Run Code Online (Sandbox Code Playgroud)
1)我也知道,因为InputStreamReader是由Reader类继承的,所以你可以通过BufferedReader构造函数传递它.但我的问题是为什么我们需要InputStreamReader?是因为InputStreamReader可以保存输入流(根据它在API中的构造函数)
编辑:我犯了错误,请忽略这一点
3)最后但并非最不重要的是,这里有关于BufferedReader和PrintWriter的另一个问题
BufferedReader inputStream = new BufferedReader(new FileReader("xanadu.txt"));
PrintWriter outputStream = new PrintWriter(new FileWriter("characteroutput.txt"));
Run Code Online (Sandbox Code Playgroud)
为什么那两个类,选择通过FileReader和FileWriter类的引用?
我想我想要更多关于为什么选择这些特定类的信息.
提前致谢!
不使用file.list()或Files.list(path),如何计算目录中的文件编号?
我只想要一个数字,没有细节。请给我一个快速的方法。
我正在尝试使用Java I/O Stream将一个文件的内容复制到另一个文件.我已经为此编写了下面的代码,但它只复制了源文件的最后一个字母.
package io.file;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyTester {
public void copyFile() {
FileInputStream fis = null;
try{
fis = new FileInputStream("resources/Source.txt");
System.out.println("Started copying");
int data = fis.read();
while (data != -1){
try (FileOutputStream fos = new FileOutputStream("resources/Destination.docx")){
fos.write(data);
fos.close();
}
catch (IOException io) {
System.err.println("Error o/p:"+io.getMessage());
}
System.out.print((char)data+" ");
data = fis.read();
}
fis.close();
System.out.println("End Copying");
}
catch(IOException ioe){
System.err.println("ERROR: "+ioe.getMessage());
}
}
public static void main(String[] args) {
new FileCopyTester().copyFile();
}
} …Run Code Online (Sandbox Code Playgroud) java ×10
java-io ×10
binaryfiles ×1
file-io ×1
inheritance ×1
model ×1
newline ×1
printwriter ×1
xml ×1