标签: java

FileInputStream与FileReader

FileReader rd=new FileReader("new.mp4");
FileWriter wr=new FileWriter("output.mp4");
int ch;
while((ch=rd.read())!=-1)
  wr.write(ch);

wr.flush();
wr.close();
Run Code Online (Sandbox Code Playgroud)

当我使用FileReaderFileWriter读取和写入mp4文件时,output.mp4文件无法很好地呈现.但是当我使用它时FileInputStream,FileOutputStream它运作良好.

那么我可以得出结论FileReader,FileWriter仅用于阅读和撰写文本吗?

java io writer

42
推荐指数
3
解决办法
4万
查看次数

Android - 将@IntDef值放在@interface中可以吗?

我正在尝试@IntDef在Android开发中实现注释.

第一种方法:在Constant.java类中分隔定义看起来很棒:

public class Constant {
   @IntDef(value={SORT_PRICE, SORT_TIME, SORT_DURATION})
   @Retention(RetentionPolicy.SOURCE)
   public @interface SortType{}
   public static final int SORT_PRICE = 0;
   public static final int SORT_TIME = 1;
   public static final int SORT_DURATION = 2;
}
Run Code Online (Sandbox Code Playgroud)

用法:

@Constant.SortType int sortType = Constant.SORT_PRICE;
Run Code Online (Sandbox Code Playgroud)

但是当在一个文件中存在多个定义(例如UserType,StoreType等)时,事情会变得更加混乱.

第二种方法:所以我想出了这样的东西来分隔定义之间的值:

public class Constant {
   @IntDef(value={SortType.SORT_PRICE, SortType.SORT_TIME, SortType.SORT_DURATION})
   @Retention(RetentionPolicy.SOURCE)
   public @interface SortTypeDef{}

   public static class SortType{
       public static final int PRICE = 0;
       public static final int TIME = 1;
       public static …
Run Code Online (Sandbox Code Playgroud)

java android annotations

42
推荐指数
1
解决办法
3807
查看次数

使用java8流创建另一个对象列表

我正在尝试理解Java 8流.我有两节课:

public class UserMeal {
    protected final LocalDateTime dateTime;

    protected final String description;

    protected final int calories;

    public UserMeal(LocalDateTime dateTime, String description, int calories) {
        this.dateTime = dateTime;
        this.description = description;
        this.calories = calories;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public String getDescription() {
        return description;
    }

    public int getCalories() {
        return calories;
    }
}
Run Code Online (Sandbox Code Playgroud)

和:

public class UserMealWithExceed {
    protected final LocalDateTime dateTime;

    protected final String description;

    protected final int calories;

    protected final boolean exceed;

    public …
Run Code Online (Sandbox Code Playgroud)

java java-stream

42
推荐指数
3
解决办法
13万
查看次数

AWS lambda和Java并发

众所周知,AWS lambda 可能会重用早期创建的处理程序对象,而且确实如此(请参阅常见问题解答):

问:AWS Lambda会重用函数实例吗?

为了提高性能,AWS Lambda可以选择保留您的函数实例并重复使用它来提供后续请求,而不是创建新副本.您的代码不应该假设这总是会发生.


问题是关于Java并发性.如果我有一个处理程序的类,请说:

public class MyHandler {
    private Foo foo;
    public void handler(Map<String,String> request, Context context) {
       ...
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,foo在这里访问和使用对象变量是否可以线程安全?

换句话说:AWS lambda可以同时为不同的调用使用相同的对象吗?

编辑我的函数在基于事件的源上处理,特别是它由API网关方法调用.

EDIT-2当你想要将某种连接池实现到外部资源时,这类问题就会出现,所以我想保持与外部资源的连接作为对象变量.它实际上按预期工作,但我害怕并发问题.

EDIT-3更具体地说,我想知道:AWS lambda处理程序的实例是否可以共享公共堆(内存)?我必须指定这个额外的细节,以防止回答列出有关java线程安全对象的明显和常见的事情.

java concurrency multithreading amazon-web-services aws-lambda

42
推荐指数
1
解决办法
1万
查看次数

为什么这个通用代码在java 8中编译?

我偶然发现了一段代码,让我想知道为什么它成功编译:

public class Main {
    public static void main(String[] args) {
        String s =  newList(); // why does this line compile?
        System.out.println(s);
    }

    private static <T extends List<Integer>> T newList() {
        return (T) new ArrayList<Integer>();
    }
}
Run Code Online (Sandbox Code Playgroud)

有趣的是,如果我修改方法的签名newList<T extends ArrayList<Integer>>它不工作了.

注释和响应后更新: 如果我将泛型类型从方法移动到类,则代码不再编译:

public class SomeClass<T extends List<Integer>> {
    public  void main(String[] args) {
        String s = newList(); // this doesn't compile anymore
        System.out.println(s);
    }

    private T newList() {
        return (T) new ArrayList<Integer>();
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics compiler-errors java-8

42
推荐指数
3
解决办法
4254
查看次数

Kotlin酒店的私人吸气剂和公共二传手

如何在Kotlin建造一个拥有私人吸气剂(或者只是没有它)但拥有公共二传手的物业?

var status
private get
Run Code Online (Sandbox Code Playgroud)

不适用于错误: Getter visibility must be the same as property visibility

在我的情况下,原因是Java互操作:我希望我的Java代码能够调用setStatus但不能getStatus.

java kotlin kotlin-interop

42
推荐指数
3
解决办法
9951
查看次数

是否有类可选但非选项类?

声明函数来映射值并在它们存在时使用它们是很方便的.

在你有几个强制对象和几个Optionals的情况下,我发现自己将其他包装在Optional.of(mandatoryObject)中,所以我可以在它们上使用相同的表达式而不用向后写它们.

Food vegetables = Food.someVegetables();
Optional<Food> condiment = Food.someCondiment();
Optional<Food> spices = Food.someSpices();

condiment.map(prepare).ifPresent(putOnPlate);
spices.map(prepare).ifPresent(putOnPlate);
Run Code Online (Sandbox Code Playgroud)

但后来我不喜欢这段代码:

putOnPlate.accept(prepare.apply(vegetables));
Run Code Online (Sandbox Code Playgroud)

所以我把它包起来:

Optional.of(vegetables).map(prepare).ifPresent(putOnPlate);
Run Code Online (Sandbox Code Playgroud)

但这是错误的,因为蔬菜(在这个例子中)实际上不是可选的.它们非常重要,我给大家的印象是它们是可选的.

所以我的问题是:java中有一些类如java.util.Mandatory,所以我可以写:

Mandatory.of(vegetables).map(prepare).definitelyPresentSo(putOnPlate);
Run Code Online (Sandbox Code Playgroud)

java optional java-8

42
推荐指数
2
解决办法
1891
查看次数

Cordova无法为2097152KB对象堆保留足够的空间

我是cordova的新手,我正在尝试创建一个android HelloWold项目.

当我使用时cordova platforms add android,它抛出一些异常:

D:\CordovaSpace\helloWorld>cordova platforms add android
Adding android project...
Creating Cordova project for the Android platform:
        Path: platforms\android
        Package: com.example.helloworld
        Name: HelloWorld
        Activity: MainActivity
        Android target: android-24
Subproject Path: CordovaLib
Android project created with cordova-android@6.0.0
Installing "cordova-plugin-whitelist" for android
ANDROID_HOME=D:\Java_Android_SDK\android_sdk
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_73
Subproject Path: CordovaLib
Starting a new Gradle Daemon for this build (subsequent builds will be faster).

FAILURE: Build failed with an exception.

* What went wrong:
Unable to start the daemon …
Run Code Online (Sandbox Code Playgroud)

java android cordova

42
推荐指数
4
解决办法
3万
查看次数

UnsatisfiedDependencyException:使用name创建bean时出错

有几天我正在尝试创建Spring CRUD应用程序.我糊涂了.我无法解决这个错误.

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientController'的bean时出错:通过方法'setClientService'参数0表示不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientService'的bean时出错:通过字段'clientRepository'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'com.kopylov.repository.ClientRepository'的限定bean可用:预期至少有1个bean有资格作为autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

还有这个

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'clientService'的bean时出错:通过字段'clientRepository'表示的不满意的依赖关系; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型'com.kopylov.repository.ClientRepository'的限定bean可用:预期至少有1个bean有资格作为autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

ClientController

@Controller
public class ClientController {
private ClientService clientService;

@Autowired
@Qualifier("clientService")
public void setClientService(ClientService clientService){
    this.clientService=clientService;
}
@RequestMapping(value = "registration/add", method = RequestMethod.POST)
public String addUser(@ModelAttribute Client client){
    this.clientService.addClient(client);
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)

ClientServiceImpl

@Service("clientService")
public class ClientServiceImpl implements ClientService{

private ClientRepository clientRepository;

@Autowired
@Qualifier("clientRepository")
public void setClientRepository(ClientRepository clientRepository){
    this.clientRepository=clientRepository;
}



@Transactional
public void addClient(Client client){
    clientRepository.saveAndFlush(client);
}
}
Run Code Online (Sandbox Code Playgroud)

ClientRepository

public interface ClientRepository extends JpaRepository<Client, Integer> …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc

42
推荐指数
4
解决办法
26万
查看次数

为什么Java 12会尝试将切换结果转换为数字?

我同意以下代码:

var y = switch (0) {
    case 0 -> '0';
    case 1 -> 0.0F;
    case 2 -> 2L;
    case 3 -> true;
    default -> 4;
};
System.out.println(y);
System.out.println(((Object) y).getClass().getName());
Run Code Online (Sandbox Code Playgroud)

返回此:

0
java.lang.Character
Run Code Online (Sandbox Code Playgroud)

但是,如果删除布尔值:

var y = switch (0) {
    case 0 -> '0';
    case 1 -> 0.0F;
    case 2 -> 2L;
    default -> 4;
};
System.out.println(y);
System.out.println(((Object) y).getClass().getName());
Run Code Online (Sandbox Code Playgroud)

返回此:

48.0
java.lang.Float
Run Code Online (Sandbox Code Playgroud)

我想这个结果是出乎意料的。

java switch-statement java-12

42
推荐指数
1
解决办法
1933
查看次数