Skip to content

Instantly share code, notes, and snippets.

InputStream inputStream = null;
byte[] dataChunk = new byte[1024];
try {
inputStream = new FileInputStream("test.txt");
while (inputStream.read(dataChunk) != -1) {
//Perform operations on data...
}
} catch (IOException e) {
e.printStackTrace();
byte[] dataChunk = new byte[1024];
try (InputStream inputStream = new FileInputStream("test.txt")) {
while (inputStream.read(dataChunk) != -1) {
//Perform operations on data...
}
} catch (IOException e) {
e.printStackTrace();
}
String path = "tmp/documents";
String fileName = "test.txt";
File targetFile = new File(path, fileName);
InputStream inputStream = new FileInputStream(targetFile);
private String getStringFromStream(InputStream inputStream) throws IOException {
StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader(inputStream))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}
return textBuilder.toString();
}
private String getStringFromStream(InputStream inputStream) throws IOException {
try (final Reader reader = new InputStreamReader(inputStream)) {
return CharStreams.toString(reader);
}
}
private String getStringFromStream(InputStream inputStream) throws IOException {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
}
@Transactional
public Client saveNewClient(ClientDto clientDto) {
Client client = new Client();
client.setName(clientDto.getName());
client.setEmail(clientDto.getEmail());
Client newClient = clientRepository.save(client);
return newClient;
}
@Transactional
public void updateClientsName(String oldName, String newName) {
Client managedClient = clientRepository.findByName(oldName);
managedClient.setName(newName);
}
@Service
public class ClientsService {
private final ClientRepository clientRepository;
@Autowired
public ClientsService(ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@Transactional
public void deleteClientWithName(String name) {
Client managedClient = clientRepository.findByName(name);
clientRepository.delete(managedClient);
}