我在使用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执行显示,在方法映射(..)FileChannelImpl取size型的long作为输入.但是在体内,它比较它Integer.MAX_VALUE并且如果它大于那个则抛出错误.为什么将long尺寸作为输入但是将其限制为最大integer长度?
有谁知道这个实现背后的具体原因?还是某种虫子?
我在64位Windows-2k8上使用64位JRE运行此程序
我正在尝试通过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) 我有兴趣将我收到的一些数据写入两个不同的文件(相同的数据).
在我的代码中,我正在使用BufferedWriter并将FileWriter数据写入文件,我希望,作为备份,在本地存储和SD卡上写入相同的数据.
我的问题是,如果我需要用2 FileWriters和2 来实现它BufferedWriters,还是有办法BufferedWriter对两个文件使用相同的?
有没有更有效的方法来实现这项任务?
我正在尝试以编程方式将文件从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)
这两个都不起作用..有
什么建议吗?
有一些例子,人们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)