मैं इसके उत्तर के लिए चारों ओर देख रहा हूं, लेकिन वास्तव में कुछ भी नहीं मिला। इससे पहले, मैंने पूछा कि मैं बाद में पुनर्प्राप्ति के लिए एक बाइट सरणी के माध्यम से एक स्ट्रिंग में फ़ाइल कैसे बना सकता हूं, और फिर फिर से वापस कर सकता हूं।BufferedInputStream बाइट [] में एक सॉकेट पर एक सॉकेट पर भेजने के लिए
लोगों ने मुझे क्या बताया, यह था कि मुझे बुरा एन्कोडिंग समस्याओं से बचने के लिए बाइट सरणी को स्टोर करना था। तो अब मैंने उस पर काम करना शुरू कर दिया है, लेकिन अब मैंने एक दीवार मारा है।
मूल रूप से, मैंने एक बाइट सरणी में फ़ाइल को चालू करने के लिए पहले unbuffered स्ट्रीम का उपयोग किया था। यह सिद्धांत में अच्छा काम करता है, लेकिन इसमें बहुत सारी मेमोरी होती है जो अंततः ढेर आकार अपवाद डालेगी। मुझे इसके बजाय buffered स्ट्रीम का उपयोग करना चाहिए (या तो मुझे बताया गया है), और अब मेरी समस्या है, BufferedInputStream से एक बाइट [] पर जा रहा है। मैं कॉपी और इस दस्तावेज़
मैं बफ़र धाराओं के लिए unbuffered धाराओं कहाँ का आदान-प्रदान में तरीकों पाया उपयोग करने के लिए कोशिश की है। एकमात्र मुद्दा यह है कि मैं एक बफर्ड आउटपुट स्ट्रीम को बाइट सरणी में सीधे नहीं बदल सकता, क्योंकि मैं एक अप्रयुक्त स्ट्रीम के साथ कर सकता हूं।
सहायता? :)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public final class BufferedByteStream {
private static final int BUF_SIZE = 1024000;
public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException {
byte[] buf = new byte[BUF_SIZE];
long total = 0;
while(true) {
int r = from.read(buf);
if(r == -1) {
break;
}
to.write(buf, 0, r);
total += r;
}
return total;
}
public static byte[] toByteArray(BufferedInputStream in) throws IOException {
BufferedOutputStream out = new BufferedOutputStream(new ByteArrayOutputStream());
copy(in, out);
return out. // <--- Problem is here
}
}
संपादित करें:
मैं अभी भी हो रही है ढेर अंतरिक्ष त्रुटियों।
main.java
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import jserver.io.BufferedByteStream;
/**
*
* @author Vipar
*/
public class main {
public static void main(String[] args) {
File f = new File("<doesn't matter>");
try {
byte[] buf;
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f))) {
buf = BufferedByteStream.toByteArray(bis);
bis.close();
}
File f2 = new File("<doesn't matter>");
try (FileOutputStream fos = new FileOutputStream(f2)) {
fos.write(buf);
fos.close();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
BufferedByteStream: तो अब मैं सभी कोड पोस्ट करेंगे।जावा
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public final class BufferedByteStream {
private static final int BUF_SIZE = 1024000;
public static long copy(BufferedInputStream from, BufferedOutputStream to) throws IOException {
byte[] buf = new byte[BUF_SIZE];
long total = 0;
while(true) {
int r = from.read(buf);
if(r == -1) {
break;
}
to.write(buf, 0, r);
total += r;
}
return total;
}
public static byte[] toByteArray(BufferedInputStream in) throws IOException {
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
BufferedOutputStream out = new BufferedOutputStream(bytesOut);
copy(in, out);
return bytesOut.toByteArray();
}
}
आधे समाधान के साथ क्या गलत है, यह पूछने के बजाय आप हमें बताएंगे कि आप किस समस्या को हल करने की कोशिश कर रहे हैं? – EJP
मुझे मूल रूप से अब मुझे एहसास होना चाहिए था, लेकिन मैं इसे एक उचित प्रश्न में करूँगा जहां मैंने बस यह निर्धारित किया कि मैं क्या हासिल करना चाहता हूं। – OmniOwl