在java中,如何从输入流中读取固定长度并保存为文件?例如。我想从 inputStream 读取 5M,并保存为 downloadFile.txt 或其他内容。(BUFFERSIZE=1024)
FileOutputStream fos = new FileOutputStream(downloadFile);
byte buffer [] = new byte[BUFFERSIZE];
int temp = 0;
while ((temp = inputStream.read(buffer)) != -1)
{
fos.write(buffer, 0, temp);
}
Run Code Online (Sandbox Code Playgroud) java inputstream outputstream fileinputstream fileoutputstream
我想将 HTTP 请求的响应写入文件。但是,我想将响应流式传输到物理文件,而无需等待加载整个响应。
我实际上将向 JHAT 服务器发出请求,以从转储中返回所有字符串。我的浏览器在响应完成之前挂起,因为有 70k 个这样的对象,我想将它们写入一个文件,以便我可以扫描。
提前致谢,
我试图理解Oreilly的Java IO中的以下代码.它应该从文件中读取并将其写入控制台:
try {
FileInputStream fis = new FileInputStream("README.TXT");
int n;
while ((n = fis.available()) > 0) {
byte[] b = new byte[n];
int result = fis.read(b);
if (result == -1) break;
String s = new String(b);
System.out.print(s);
} // End while
} // End try
catch (IOException e) {System.err.println(e);}
System.out.println();
Run Code Online (Sandbox Code Playgroud)
我的问题是:
该available方法将立即找到可用的最大长度,然后read可以调用该方法将其打印出来.这应该在一个调用中完成,为什么作者在while循环中执行它,连续检查可用性?
public class JavaCopyFileProgram {
public static void main(String[] args)
{
File sourceFile = new File("F:/Study/Java/Java Programs/Factory Methods.txt");
File destFile = new File("D:/DestFile.txt");
FileInputStream inStream = null;
FileOutputStream outStream = null;
try
{
inStream = new FileInputStream(sourceFile);
outStream = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inStream.read(buffer)) != -1)
{
outStream.write(buffer, 0, length);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
inStream.close();
outStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
System.out.println("Success"); …Run Code Online (Sandbox Code Playgroud) 我想.txt使用 Java Stream在文件中查找字谜。这是我所拥有的:
try (InputStream is = new URL("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt").openConnection().getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
Stream<String> stream = reader.lines()) {
Run Code Online (Sandbox Code Playgroud)
以及字谜的方法:
public boolean isAnagram(String firstWord, String secondWord) {
char[] word1 = firstWord.replaceAll("[\\s]", "").toCharArray();
char[] word2 = secondWord.replaceAll("[\\s]", "").toCharArray();
Arrays.sort(word1);
Arrays.sort(word2);
return Arrays.equals(word1, word2);
}
Run Code Online (Sandbox Code Playgroud)
如何使用 Java 8 Stream 检查 unixdict.txt 中的单词是否是字谜?有没有办法将一个词与流中的所有词进行比较?
我正在尝试读取文本文件,我正在使用fileImputStream,并将所有行读入单个String,然后将其输出到控制台(System.out)
当我尝试阅读humanSerf.txt时,它在consol中给了我这个:
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 symbol=HS\
strength=15\
agility=13\
constitution=7\
wisdom=9\
intelligence=5}
Run Code Online (Sandbox Code Playgroud)
在文本文件中,它说:
symbol=HS
strength=15
agility=13
constitution=7
wisdom=9
intelligence=5
Run Code Online (Sandbox Code Playgroud)
如何让奇怪的文字消失?
这是我正在使用的代码,请帮忙
try{
// Open the file that is the first
// command line parameter
FileInputStream read = new FileInputStream("resources/monsters/human/humanSerf.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(read);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the …Run Code Online (Sandbox Code Playgroud) 我试图用C++中的文本文件输入数据.文本文件采用以下格式:
4 15
3 516
25 52 etc.
Run Code Online (Sandbox Code Playgroud)
每行包含两个整数.我不知道文件中的行数,所以我可以绑定足够的内存,这就是我作为解决方法的方法:
ifstream filein;
filein.open("text.txt",ios::in);
int count=0;
while (!filein.eof())
{
count++;
filein>>temporary;
}
count=count/2; // This is the number of lines in the text file.
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法想出一种重置方法
FILEIN
进入初始状态(到文件的开始,所以我实际上可以输入数据),而不是关闭输入流并再次打开它.还有其他办法吗?
我试图将一些文件添加到ZIP文件,它创建文件但不添加任何内容.代码1:
String fulldate = year + "-" + month + "-" + day + "-" + min;
File dateFolder = new File("F:\\" + compname + "\\" + fulldate);
dateFolder.mkdir();
String zipName = "F:\\" + compname + "\\" + fulldate + "\\" + fulldate + ".zip";
zipFolder(tobackup, zipName);
Run Code Online (Sandbox Code Playgroud)
我的功能:
public static void zipFolder(File folder, String name) throws Exception {
byte[] buffer = new byte[18024];
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(name));
FileInputStream in = new FileInputStream(folder);
out.putNextEntry(new ZipEntry(name));
int len;
while((len = …Run Code Online (Sandbox Code Playgroud) 我试图用Java读取一个名为"KFormLList.txt"的文件.它与此程序一起保存在默认包中,但是当我尝试运行它时,我收到以下错误消息:"错误:KFormLList.txt(系统找不到指定的文件)"
我究竟做错了什么?谢谢你的帮助.
import java.io.*;
public class VLOCGenerater {
/**
* @param args
*/
public static void main(String[] args) {
try {
//Read the text file "KFormLList.txt"
FileInputStream fis = new FileInputStream("KFormLList.txt");
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
String strLine;
int V = 0;
int LOC = 0;
while((strLine = br.readLine())!= null ){
if (strLine.trim().length() != 0){
System.out.println(strLine);
V++;
}
else {
LOC++;
}
}
System.out.println("V = " + V);
System.out.println("LOC = " + LOC);
dis.close(); …Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,其中包含我的计算机上保存的内容abcdefgh.我想使用FileInputStream来显示控制台上的字符,同时还要测量执行此操作所需的时间.它看起来像这样:
public class Readtime {
public static void main(String args[]) throws Exception{
FileInputStream in=new FileInputStream("bolbol.txt");
while(in.read()!=-1){
long startTime = System.nanoTime();
int x = in.read();
long endtime = System.nanoTime();
System.out.println(endtime-startTime);
System.out.println((char)x);
}
in.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我在控制台上得到的是以下内容:
8863
b
7464
d
6998
f
6997
h
Run Code Online (Sandbox Code Playgroud)
其余的字母现在在哪里?就好像只进行了4次读操作一样.我的想法是朝着字符大小的方向前进,一次read()只能读取一个字节,但我没有到达任何地方.
fileinputstream ×10
java ×8
file-io ×2
inputstream ×2
anagram ×1
c++ ×1
directory ×1
httpresponse ×1
io ×1
java-io ×1
java-stream ×1
outputstream ×1
stream ×1
zip ×1