我正在尝试从Android应用程序中的Web URL保存图像,但是当我运行它时,日志cat会抛出一个异常,说它是"只读".我不知道最近发生了什么.
这是我的下载类:
public class ImageDownload {
public static void downloader(String imageURL, String fileName) {
try {
URL url = new URL("http://www.exampleurl.com/" + imageURL);
File file = new File(fileName);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (IOException e) {
Log.d("Downloader", "Error: " + e);
}
} …Run Code Online (Sandbox Code Playgroud) android urlconnection filenotfoundexception fileoutputstream
我有一个应用程序,使用从Socket InputStream获取的字节数组创建多个文件.当我只保存一个文件时,文件保存完美,但如果我保存一个文件然后重新实例化文件流并保存另一个文件,则第一个文件被破坏,第二个文件被完美保存.我在文本编辑器中打开了两个文件,似乎(约......)第一个文件的前1/5是空格但第二个文件已满,它们都具有相同的大小属性(9,128,731字节).以下示例是senario的重复,但具有相同的损坏结果:
FileOutputStream outStream;
outStream = new FileOutputStream("/mnt/sdcard/testmp3.mp3");
File file = new File("/mnt/sdcard/test.mp3");
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[9128731];
inStream.read(buffer);
outStream.write(buffer, 0, buffer.length);
inStream.close();
outStream.flush();
outStream.close();
outStream = null;
outStream = new FileOutputStream("/mnt/sdcard/testmp32.mp3");
outStream.write(buffer, 0, buffer.length);
inStream.close();
outStream.flush();
outStream.close();
outStream = null;
Run Code Online (Sandbox Code Playgroud)
我在常规Java应用程序中尝试了这个EXACT代码,并且两个文件都保存没有问题.有谁知道为什么android会这样做?
任何帮助将不胜感激
我正在使用FileOutputStream将我的内容写入Android设备SD卡上的文件"sample.txt"
但每次我尝试编写内容时,FileOutputStream都会将其写入磁盘上的其他位置.
我检查文件"sample.txt"的开始位置,每次增加512字节!
我该怎么办?
代码优先:
AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets();
try {
Log.e("Glenn:", address);
FileOutputStream fout = mgr.openFd("device/device_address.txt").createOutputStream();
PrintWriter _fout = new PrintWriter(fout);
_fout.println(address);
Log.e("Glenn", address);
_fout.close();
fout.close();
InputStream fin = mgr.open("device/device_address.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
address = br.readLine();
try {
Log.e("Glenn:", address);
} catch (NullPointerException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
Log.e("Glenn", "error with OutputStream");
}
Run Code Online (Sandbox Code Playgroud)
前两次Log.e()调用打印的地址值是正确的值,实际上是设备MAC地址.但是,当我试图测试从刚刚写入的文件中读取的地址值时,NullPointerException已经在Log.e()调用中捕获了.这意味着从文件中读取的值是NULL.任何人都可以指出代码有什么问题吗?
android inputstream fileoutputstream bufferedreader android-assets
我正在使用AESCrypt开源库来加密文件.这是我的代码
String password="asdfgh";
String frompath=new String("C:/Users/sabertooth/Desktop/error/pnberror.png");
String topath=new String("C:/Users/sabertooth/desktop/error/");
AESCrypt aes=new AESCrypt(false,password);
aes.encrypt(2, frompath, topath);
Run Code Online (Sandbox Code Playgroud)
我是geeting访问被拒绝的例外.这是堆栈跟踪
Exception in thread "main" java.io.FileNotFoundException: C:\Users\sabertooth\desktop\error (Access is denied)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:212)
at java.io.FileOutputStream.<init>(FileOutputStream.java:104)
at AESCrypt.encrypt(AESCrypt.java:284)
at NewClass.main(NewClass.java:28)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Run Code Online (Sandbox Code Playgroud)
发布aescrypt.java的代码
/*
* Copyright 2008 Vócali Sistemas Inteligentes
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may …Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码(在许多答案中找到):
InputStream myInput;
try {
myInput = iNezamApplication.getAppContext().getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是,在到达OutpoutStream行后,我总是遇到异常:
java.io.FileNotFoundException: /data/data/package_name/databases/databasename.db: open failed: ENOENT (No such file or directory)
Run Code Online (Sandbox Code Playgroud) 我的目标是从文件中读取和写入对象(使用文件I/O).这些对象是存储在动态中的userinput(name,int value,double value等..)arrayList.将arrayList在我的主类中声明.作为我程序的进一步改进,我想将这些数据保存在一个文件中,我已经创建了另一个类ReaderWriter来实现file I/O.
现在,我怎么能得到的参考arrayList在ReaderWriter上课吗?
我的申请很大.我将展示与我的问题相关的部分内容:
主要:
public static void main(String[] args) {
System.out.println("WELCOME TO OUR BANK!\n\n");
List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
}
Run Code Online (Sandbox Code Playgroud)
getter和setter的类:
public class BankAccount {
private String name;
private int accNum;
private double initiateAmount;
//constructor
public BankAccount() {
this.name = null;
this.accNum = 0;
this.initiateAmount = 0;
}
public void setName(String name) {
this.name = name;
.........................
.......................
............
Run Code Online (Sandbox Code Playgroud)
读写器:
public void writeToFile(){
try { …Run Code Online (Sandbox Code Playgroud) 我知道当我想写一个文件时,我应该使用这样的代码:
InputStream inputStream = new BufferedInputStream(url.openStream());
OutputStream outputStream = new FileOutputStream("/sdcard/myfile.jpg");
byte Data[] = new byte[1024];
long total = 0;
while ((count=inputStream.read(Data)) != -1) {
total = total+count;
outputStream.write(Data,0,count);
}
Run Code Online (Sandbox Code Playgroud)
但我无法理解该结果会发生什么,是否将图像文件从零写入100?
谁能形容我这是怎么发生的?谢谢
我正在尝试编写一个程序,将一个节点保存到.txt中,但是当我执行程序时,它编写的内容较少(有时甚至没有).这是代码:
public static void main(String[] args) {
GeneradorSeñal gs = new GeneradorSeñalSinusoidal("sgsg");
Double salida;
try{
FileOutputStream out = new FileOutputStream("out.txt");
OutputStreamWriter muestras = new OutputStreamWriter(out);
for(int i=0; i<500; i++){
salida=gs.getSalida();
muestras.write(String.valueOf(salida)+"\n");
System.out.println(i+"\t"+salida);
}
}catch(Exception e){
e.getStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
GeneradorSeñal和GeneradorSeñalSinusoidal是创造正弦曲线的类.gs.getSalida()返回正弦曲线的值,正弦曲线类运行良好,这不是问题.
我的班级java程序有问题.我必须创建一个程序,提示用户输出文件的路径和名称,该文件将包含我的程序将采用的方程系数,并使用二次公式计算解.到目前为止,我认为一切都是正确的,除了我的输出文件.假设我有一个包含3行系数的输入文件,我的程序将在控制台流中显示解决方案,但只会在我的输出文件中显示1行解决方案.这是我的第一个java课程,这对我来说太过分了!我在这里先向您的帮助表示感谢!
while (input.hasNext()) {
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
discriminant = Math.pow(b, 2) - 4 * a * c;
///There will be no solutions if discriminant<0
if (discriminant < 0){
System.out.println("There are no solutions.");
output.println("There are no solutions.");
}
///As with the above, if coefficent a = 0 no solutions
else if (a == 0){
System.out.println("There are no solutions.");
output.println("There are no solutions.");
}
else if (discriminant == 0){
solutionOne = (-b + Math.sqrt(discriminant)) / (2 …Run Code Online (Sandbox Code Playgroud) fileoutputstream ×10
java ×7
android ×4
file ×2
file-io ×2
arraylist ×1
database ×1
inputstream ×1
java-io ×1
sqlite ×1