相关疑难解决方法(0)

为什么FileChannel.map会占用Integer.MAX_VALUE数据?

我在使用FileChannel.map时遇到异常

Exception in thread "main" java.lang.IllegalArgumentException: Size exceeds Integer.MAX_VALUE
    at sun.nio.ch.FileChannelImpl.map(Unknown Source)
    at niotest.NioTest.readUsingNio(NioTest.java:38)
    at niotest.NioTest.main(NioTest.java:64)
Run Code Online (Sandbox Code Playgroud)

快速寻找到的OpenJDK执行显示,在方法映射(..)FileChannelImplsize型的long作为输入.但是在体内,它比较它Integer.MAX_VALUE并且如果它大于那个则抛出错误.为什么将long尺寸作为输入但是将其限制为最大integer长度?

有谁知道这个实现背后的具体原因?还是某种虫子?

源URL - http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/nio/ch/FileChannelImpl.java

我在64位Windows-2k8上使用64位JRE运行此程序

java io nio

13
推荐指数
1
解决办法
6157
查看次数

改造.java.net.ProtocolException:期望*字节但收到*

我正在尝试通过Retrofit2执行Multipart POST请求,我将API上传到自定义文件.

它随机失败,出现此异常:

W/System.err: java.net.ProtocolException: expected 154 bytes but received 634
Run Code Online (Sandbox Code Playgroud)

有人能说些什么吗?

这是我在界面中的代码:

@Multipart
@POST("recordings/{id}/{rec_id}/")
Call<ResponseBody> uploadRecording(@Path("id") String id, @Path("rec_id") String rec_id, @Part MultipartBody.Part bleFile);
Run Code Online (Sandbox Code Playgroud)

在构造函数中:

public ApiConnectionManager(Context con){
    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    OkHttpClient.Builder client = new OkHttpClient.Builder();
    HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
    loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    client.addInterceptor(loggingInterceptor);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(con.getResources().getString(R.string.api_url)) // API url is hidden
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(client.build())
            .build();

    this.companyAPI = retrofit.create(CompanyAPI.class);
}
Run Code Online (Sandbox Code Playgroud)

并在上传方法:

private void uploadFile(String id, final File bleFile) {
    MediaType MEDIA_TYPE = MediaType.parse("multipart/mixed");
    RequestBody …
Run Code Online (Sandbox Code Playgroud)

rest android retrofit2

8
推荐指数
1
解决办法
3176
查看次数

使用BufferedWriter将相同的数据写入多个文件

我有兴趣将我收到的一些数据写入两个不同的文件(相同的数据).

在我的代码中,我正在使用BufferedWriter并将FileWriter数据写入文件,我希望,作为备份,在本地存储和SD卡上写入相同的数据.

我的问题是,如果我需要用2 FileWriters和2 来实现它BufferedWriters,还是有办法BufferedWriter对两个文件使用相同的?

有没有更有效的方法来实现这项任务?

java android file filewriter bufferedwriter

4
推荐指数
1
解决办法
196
查看次数

以编程方式将文件从缓存目录移动到SDCard

我正在尝试以编程方式将文件从Android内部存储器移动到SD卡中的现有目录.
我试过两种方法.在第一个我使用File.renameTo:

String destName = externalDirPath + File.separatorChar + destFileName;
File originFile = new File(cacheDirPath + File.separatorChar + originalfileName);

originFile.renameTo(new File(destName));
Run Code Online (Sandbox Code Playgroud)

在另一个我使用Runtime.getRuntime():

Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());

String command = "cp " + cacheDirPath + "/" + originalfileName+ " " + externalDirPath + "/" + destFileName+ "\n";

os.writeBytes(command);
Run Code Online (Sandbox Code Playgroud)

这两个都不起作用..有

什么建议吗?

storage android caching move

3
推荐指数
1
解决办法
9242
查看次数

在Java中编写文件时是否需要在finally中调用close()?

有一些例子,人们close()finally写入文件时会调用它们,例如:

OutputStream out = null;
try {
    out = new FileOutputStream(file);
    out.write(data);
    out.close();
}
catch (IOException e) {
    Log.e("Exception", "File write failed: " + e.toString());
} finally {
    try {
        if (out != null) {
            out.close();
        }
    } catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}
Run Code Online (Sandbox Code Playgroud)

但也有很多 更多的例子,包括Android官方文档,他们不这样做:

try {
    OutputStream out = new FileOutputStream(file);
    out.write(data);
    out.close();
}
catch (IOException e) {
    Log.e("Exception", "File write …
Run Code Online (Sandbox Code Playgroud)

java android

3
推荐指数
1
解决办法
6694
查看次数

标签 统计

android ×4

java ×3

bufferedwriter ×1

caching ×1

file ×1

filewriter ×1

io ×1

move ×1

nio ×1

rest ×1

retrofit2 ×1

storage ×1