Put specific image in folder from resource - java

I need put one specific image from my project to specific folder in Java. Thanks for helping.
Edit:
Im creating a folder with File and the folder I'm creating i need to put an image i have in resources. Thanks for helping.

You can use NIO package in Java 7 with the class Files and the static method copy.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Main_copie {
public static void main(String[] args) {
Path source = Paths.get("data/image1.png");
Path destination = Paths.get("MyFolder/image1_copied.png");
try {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}

Do you mean to copy the file from 1 location to another? If so, then the approach would be to create a new File at the desired location, create a FileOutputStream and write everything from the original File (using an FileInputStream) to this OutputStream.
Maybe this post can help you out.

Related

Problem when executing personnal JAR File in my program

I want to create a Java class that I use to export images. This class will be exported in .JAR file which will allow me to integrate it directly into another project by passing as input parameter the name of the file and in return I would have this image in PNG format.
That's what I tried:
package militarySymbolsLibrary;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MainEntry {
public static void main(String agrs[]) {
}
public static BufferedImage generateImage(String symbolCode) {
File imageFile = new File("/pictures/SSGPU-------.png");
BufferedImage image;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
image = null;
e.printStackTrace();
}
return image;
}
}
Pictures are stocked in folder pictures like this treeview below:
src
MainEntry.java
pictures
SSGPU-------.png
Then I export this class in .JAR file, I add it to my other program, I pass a parameter (picture's name except for this test) and normally my class return PNG file. But I have an error telling me that the file path is not the rigth one.
How can I solve this problem?
Thank you

create .gitignore with java

I'm aware this question might be a duplicate in some sense but first hear me out.
I tried to create a code where i can create gitignore file with contents and for some reason i always end up having a file with txt extension and without name. Can someone explain this behavior and why?
Example Code:
System.out.println(fileDir+"\\"+".gitignore");
FileOutputStream outputStream = new FileOutputStream(fileDir+"\\"+".gitignore",false);
byte[] strToBytes = fileContent.getBytes();
outputStream.write(strToBytes);
outputStream.close();
You can use java.nio for it. See the following example:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class StackoverflowMain {
public static void main(String[] args) {
// create the values for a folder and the file name as Strings
String folder = "Y:\\our\\destination\\folder"; // <-- CHANGE THIS ONE TO YOUR FOLDER
String gitignore = ".gitignore";
// create Paths from the Strings, the gitignorePath is the full path for the file
Path folderPath = Paths.get(folder);
Path gitignorPath = folderPath.resolve(gitignore);
// create some content to be written to .gitignore
List<String> lines = new ArrayList<>();
lines.add("# folders to be ignored");
lines.add("**/logs");
lines.add("**/classpath");
try {
// write the file along with its content
Files.write(gitignorPath, lines);
} catch (IOException e) {
e.printStackTrace();
}
}
}
It creates the file on my Windows 10 machine without any problems. You need Java 7 or higher for it.

While creating this java program in oracle db. Im facing some errors. CAn anyone tell me the way to compile it. the code is as follows

Below code is working properly in eclipse. But I,m trying to create this source in oracle 11g Db. While creating it throws some warnings.
create or replace
and compile java source named "Noitime"
as
package com;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
public class Noitime {
public String attr(String filepath,String filename)
{
long time = System.currentTimeMillis();
FileTime fileTime = FileTime.fromMillis(time);
Path path = Paths.get(filepath,filename);
try {
Files.setLastModifiedTime(path, fileTime);
return "Success";
} catch (IOException e) {
System.err.println(e);
return "Fail";
}
}
}
You appear to have garbled the error/warning message, but I think that it is telling you that you should be using the pathname "com/Noitime.java" when compiling. The pathname you use should correspond to the full class name, and the full class name is "com.Noitime".

package javax.media does not exist

I am working on an audio player and need to add pause() and play() features in it to connect with JButtons. The problem is I am not able to import Media package as it says package does not exist. I cannot find anywhere it online to download the package. Same goes for AudioPlayer class which gives bad class file error.
you need the JMF libraries , you can get them from there , for windows there is a typic installer :
JMF Download
Based on your question,
You can down load java.media
then use
import javax.media.*;
then you can declare like
Player audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
And
audioplayer.start(); and audioplayer.stop();
Here file means where the source file saved.
NB: you can use JMF jar file
Try like this
try {
audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
} catch (IOException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
}
OR Try the sample code given below
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
public class Mp3Player {
public static void main(String[] args) throws IOException, NoPlayerException, CannotRealizeException {
// Source of song file
File f=new File("your path in which mp3 file is saved");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}
It is a late answer, but you can use the Maven dependency:
<!-- https://mvnrepository.com/artifact/javax.media/jmf -->
<dependency>
<groupId>javax.media</groupId>
<artifactId>jmf</artifactId>
<version>2.1.1e</version>
</dependency>
Following four packages will solve your problem. They contains most of helpful methods to deal with audio player.
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
You can use .stop(), start(), .play() etc. from above packages.
Hope that will help.

SFNTLY: How to convert any font that gets uploaded to "WOFF" format?

I can not find any documenration on this library (https://code.google.com/p/sfntly/). I've been taking stabs at it for 2 days now. I'm trying to convert any font that gets uploaded to "WOFF" format.
Could someone shed some light?
I successfully converted my TTF into a WOFF file by following these steps:
Download and install ant following "The Short Story" steps (http://ant.apache.org/manual/install.html#getBinary)
Download SFNTLY via SVN checkout (https://code.google.com/p/sfntly/source/checkout) and followed the steps contained into the file "sfntly\java\quickstart.txt"
Created a new java project and imported the following four jars I created following the previous steps into my project:
sfntly.jar
woffconverter.jar
guava-16.0.1.jar
I slightly tweaked display_name code which contained a few syntax mistakes.
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.common.io.Files;
import com.google.typography.font.sfntly.Font;
import com.google.typography.font.sfntly.FontFactory;
import com.google.typography.font.sfntly.data.WritableFontData;
import com.google.typography.font.tools.conversion.woff.WoffWriter;
public class Main {
public static void main(String[] args) {
WoffWriter ww = new WoffWriter();
FontFactory fontFactory = FontFactory.getInstance();
byte[] bytes;
try {
bytes = Files.toByteArray(new File("C:\\FontName.TTF"));
Font font = fontFactory.loadFonts(bytes)[0];
WritableFontData wfd = ww.convert(font);
FileOutputStream fs = new FileOutputStream("out.fnt");
wfd.copyTo(fs);
fs.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
After reading the source code of SFNTLY I am no expert in sfntly, so use my answer at your risk :).
I would convert the font with WoffWriter#convert() to writeable font data, then copy the wfd to outputstream.
WoffWriter ww = new WoffWriter();
WriteableFontData wfd = ww.convert(yourFont);
try {
FileOutPutStream fs = new FileOutputStream("out.fnt");
wfd.copyTo(fs, wfd);
fs.close();
} catch (IOException e) {
}

Categories

Resources