Hallo!,
Ich versuche Java zu lernen und hab ein Problem. Ich möchte aus meiner .jar datei eine .txt datei in den Ordner kopieren. Der haken ist nur, ich möchte die Datei nur bis zur einer bestimmten byte länge kopieren. in vb ist das "einfach", aber in java brauch ich noch hilfe.
VB-Code:
in Java konnte ich bislang nur den Code finden. Ich bin aber wohl zu doof den umzuschreiben Es wäre super nett wenn mir jemand hilft das zu lösen
Spoiler anzeigen
Ich versuche Java zu lernen und hab ein Problem. Ich möchte aus meiner .jar datei eine .txt datei in den Ordner kopieren. Der haken ist nur, ich möchte die Datei nur bis zur einer bestimmten byte länge kopieren. in vb ist das "einfach", aber in java brauch ich noch hilfe.
VB-Code:
VB.NET-Quellcode
- Public Sub Copy()
- Dim len As Integer = 858274 ' << nur bis zu den bytes kopieren :: den rest weglassen
- Dim SourceFile As New IO.FileStream("source", IO.FileMode.Open)
- KopierenMitByteLänge(SourceFile, Application.StartupPath & "\target.jar", len)
- End Sub
- Public Sub KopierenMitByteLänge(ByVal input As IO.Stream, ByVal targetFile As String, ByVal length As Integer)
- Dim buffer As Byte() = New Byte(8191) {}
- Using output As IO.Stream = IO.File.OpenWrite(targetFile)
- Dim bytesRead As Integer = 1
- While length > 0 AndAlso bytesRead > 0
- bytesRead = input.Read(buffer, 0, Math.Min(length, buffer.Length))
- output.Write(buffer, 0, bytesRead)
- length -= bytesRead
- End While
- End Using
- End Sub
in Java konnte ich bislang nur den Code finden. Ich bin aber wohl zu doof den umzuschreiben Es wäre super nett wenn mir jemand hilft das zu lösen
C#-Quellcode
- static public String ExportResource(String resourceName) throws Exception {
- InputStream stream = null;
- OutputStream resStreamOut = null;
- String jarFolder;
- try {
- stream = R78347fh97h.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
- if(stream == null) {
- throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");
- }
- int readBytes;
- byte[] buffer = new byte[4096];
- jarFolder = new File(R78347fh97h.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile().getPath().replace('\\', '/');
- resStreamOut = new FileOutputStream(jarFolder + resourceName);
- // int toReadBytes = Integer.parseInt(raw);
- while ((readBytes = stream.read(buffer)) > 0) {
- resStreamOut.write(buffer, 0, readBytes);
- }
- } catch (Exception ex) {
- throw ex;
- } finally {
- stream.close();
- resStreamOut.close();
- }
- return jarFolder + resourceName;
- }