Below, I am trying to change the value of the Path object there using the setSoundPath() method. I cannot find any documentation to say this is possible.
I am trying to create a class that will create a copy of a file at a specified path and put the copy in the specified folder. I need to be able to change the name of the path though, because I want to create the sound object with an initial placeholder file path.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.io.IOException;
import javafx.beans.property.StringProperty;
import javafx.beans.property.SimpleStringProperty;
class Scratch {
public static class Sound extends Object{
private Path there;
StringProperty tests = new SimpleStringProperty(this, "test", "");
public Sound(){
this.there = Paths.get("C:\\Users\\HNS1Lab.NETWORK\\Videos\\JuiceWRLD.mp3");
}
public void setSoundPath(String SoundPath) {
this.tests.setValue(SoundPath);
this.there = Paths.get(this.tests.toString());
}
}
public static void main(String[] args) {
Sound test = new Sound();
test.setSoundPath("C:\\Users\\HNS1Lab.NETWORK\\Music\\Meowing-cat-sound.mp3");
test.copySound();
System.out.println("Path: " + test.getSoundPath().toString());
}
}
They are immutable:
Implementations of this interface are immutable and safe for use by
multiple concurrent threads.
(from: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html)
You can create new Path objects that point to your path.
Related
I tried to add a read permission for a given file to a user on windows 10 using the AclEntry.add () method in java, the process runs normally. However, this change does not occur in windows.
I want to know if this method should assign permission permanently or only during the execution of the program.
NOTE: I am using the AclFileAttributeView interface of the Nio.File package.
In addition I packaged this jar using Launch4j to be able to run it as an administrator.
below the code I'm using.
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.List;
public class Inicial {
public Inicial() throws IOException {
Path path = Paths.get("C:\\Windows\\System32\\config\\journal");
UserPrincipal usercop =
FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName("Gilderlei");
AclFileAttributeView aclFileAttributeView = Files.getFileAttributeView(path,
AclFileAttributeView.class);
List<AclEntry> aclEntries = aclFileAttributeView.getAcl();
AclEntry.Builder aclEntry = AclEntry.newBuilder()
.setType(AclEntryType.ALLOW)
.setPrincipal(usercop)
.setPermissions(AclEntryPermission.READ_DATA);
AclEntry entry = aclEntry.build();
aclEntries.clear();
aclEntries.add(entry);
}
public static void main(String[] args) throws IOException {
new Inicial();
}
}
You just add the created permission to the aclEntries list. Java is not an ORM mapped system that automatically writes back any data changes.
Therefore you have to call at least setAcl
aclFileAttributeView.setAcl(aclEntries);
at the end of your code.
`my code is as follows:enter code here
public String createDir(String X, String Y){
String dirName = null;
try {
File dir = new File(X+Y);
Path path = FileSystems.getDefault().getPath(X+Y));
File checkPath = new File(path.getParent().toString());
boolean isDirCreated = dir.mkdir();
if(isDirCreated){
dirName = path.getFileName().toString();
}else {
dirName = null;
}
}
catch(SecurityException se){
//message
}
catch(Exception se){
//message
dirName = null;
}
return dirName;
}
`My JUnit Test Case:
In this test case I have mocked the objects of path,file.
import static org.junit.Assert.*;
import static org.easymock.EasyMock.expect;
import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import org.easymock.Mock;
import org.junit.Test;
import com.Users.Folder.Utilities;
public class UtilitiesTest {
#Mock
Utilities Utils;
#Mock
File dir,checkPath;
#Mock
Path path;
#Test
public void testCreateDir() throws Exception {
expect(dir).andReturn(new File("C:\\Users\\sasank\\Desktop\\Docs\\Docs_docready"));
expect(path).andReturn(FileSystems.getDefault().getPath("C:\\Users\\sasank\\Desktop\\Docs\\Docs_docready"));
expect(checkPath).andReturn(new File("C:\\Users\\sasank\\Desktop\\Docs"));
replay(dir,path,checkPath);
assertEquals(true, isDirCreated); //isDirCreated cannot be resolved to a variable
verify(dir,path,checkPath);
assertEquals("Docs_docready", imiUtils.dirName); // dirName cannot be resolved or is not a field
}
`dirName is also a boolean type.
what my code does is it creates a new directory in the input directory.
The "expect" method expects a call for a method from an object, which is replaced by returning value when you call the "andRetuns()". It is used for mocking classes/interfaces behaviors and does not process plain values.
For values comparison, you should use "assertEquals()" or, in your case "assertTrue()" methods in JUnit.
Simply do:
assertTrue(isDirCreated);
or
assertEquals(true, isDirCreated);
in your tests to verify the condition.
EDIT:
Seeing your code and how it behaves, here are some comments:
I think you don't need Mocks;
For a better understanding about what Mocks are and how they behave, you should read: http://easymock.org/getting-started.html
"isDirCreated" is a local variable and is never visiblie outside of "createDir" method. If you want it visible, you should create an attribute in your class and write a getter instead;
A simpler way to do your test is (assuming that "createDir" method is in a class named "Dir". If not, simply replace "Dir" with your current class name):
#Test
public void testCreateDir() throws Exception {
Dir dir = new Dir();
String directory = dir.createDir("c:/", "test_dir");
assertNotNull(directory); // this one checks if your method returned the correct path
File file = new File(directory);
assertTrue(file.exists()); // this one checks if your method created correctly you directory
}
Hope this helps.
I am trying to add a texture to an item, yet the texture just doesn't appear. I have the texture made, and in the right file directory, but while in game it doesn't display. Thus, I think it's an error in my code.
For the whole class file, see below:
package Moonstone;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.EnumHelper;
#Mod(modid = "ms", name = "Moonstone", version = "1.0")
public class MoonstoneMain {
public static Item moonstone;
#EventHandler
public void preInit(FMLPreInitializationEvent event) {
//All init
moonstone = new Moonstone().setUnlocalizedName("Moonstone").setTextureName("moonstone").setMaxStac kSize(64);
GameRegistry.registerItem(moonstone, moonstone.getUnlocalizedName().substring(5));
}
#EventHandler
public void init(FMLInitializationEvent event) {
//Proxy, TileEntity, entity, GUI and packet registering
}
#EventHandler
public void postInit(FMLPostInitializationEvent event) {
}
public static CreativeTabs tabMoonstone = new CreativeTabs("tabMoonstone"){
#Override
public Item getTabIconItem(){
return new ItemStack(Items.stick).getItem();
}
};
}
For just the item, look below-
moonstone = new Moonstone().setUnlocalizedName("Moonstone").setTextureName("moonstone").setMaxStackSize(64);// I have tried with ms:moonstone and without, both don't work.
GameRegistry.registerItem(moonstone, moonstone.getUnlocalizedName().substring(5));
Recommended changes but not necessary:
First:
When registering the item you should remove the .substring(5),
having this in will name the item "Moons" instead of "Moonstone".
Second:
unlocalized names should always be lowercase and should be formatted
as modid_itemname
Third:
Package names should be lowercase, package moonstone
Fourth:
Your should make a Refstrings.java file and put the modid, version and name in it
package Moonstone
public RefStrings{
public static String NAME = "Moonstone";
public static String MODID = "ms";
public static String VERSION = "1.0";
}
Necessary changes:
Your setTextureName should be passed "ms:moonstone"
You didn't post your folder structure but it should look like this:
src/main/java/Moonstone/MoonstoneMain.java
src/main/resources/assests/ms/textures/items/moonstone.png
it is possible that some of the recommend changes will be what fixes the problem, minecraft can be a bit finicky with naming.
I know C++ at a decent level and I am trying to learn java. This will be a silly question but I cannot figure out how to import a .java file into another. I am at Eclipse IDE and in my project I have two files:
FileReader.java
Entry.java
I want to import the Entry.java in the other file but no matter what I do I get an error. Can you help me? Thx in advance.
FileReader.java :
import java.io.*;
class FileReader {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World");
Entry a(10,"a title","a description");
a.print();
}
}
Entry.java:
public class Entry{
int ID;
String title;
String description;
public Entry(int id, String t,String d){
ID=id;
title=t;
description=d;
}
public void print(){
System.out.println("ID:"+ID);
System.out.println("Title:"+title);
System.out.println("Description:"+description);
}
}
At this state I get an error that Entry cannot be resolved as a variable. So I believe that it is related to the import.
Firstly
Entry a(10,"a title","a description");
should be
Entry a = new Entry (10,"a title","a description");
If Entry is in the same package then you will not need to import it.
If Entry is in a different package, say com.example then you will need to do
Either
import com.example.Entry;
or
import com.example.*;
The second import will import all classes in the com.example package - usually not such a good thing.
You need new Entry
The new keyword creates the new object
Entry a = new Entry(10,"a title","a description")
a.print();
An Entry object is created with the a reference with the above instantiation.
For the import part of your question, if two files are in the same package, no import is needed. If you Entry class was in a different package than your FileReader class, then you would need to import mypackage.Entry
Try
Entry a = new Entry(/*args*/);
And if you need to import the class, then use the absolute name (package+class) and put it after import above the class declaration
import com.example.you.Entry;
In Eclipse you can do Ctrl+Shift+O to resolve all imports.
Using a sample from xSocket which will run xSocketHandler as a new process, I want to customize and moving all of these code into other java file, can I copy public class xSocketDataHandler implements IDataHandler and paste into different filename say main.java?
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.channels.ClosedChannelException;
import org.xsocket.*;
import org.xsocket.connection.*;
public class xSocketDataHandler implements IDataHandler
{
public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException
{
try
{
String data = nbc.readStringByDelimiter("\0");
//nbc.write("Reply" + data + "\0");
nbc.write("+A4\0");
if(data.equalsIgnoreCase("SHUTDOWN"))
xSocketServer.shutdownServer();
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
return true;
}
}
No, you can't do that without reducing the visibility of xSocketDataHandler to default. If you don't want to do that, your file name should be xSocketDataHandler.java
You must be having class xSocketDataHandler in a file of the same name already since it is public. You could move other non public classes in this file to Main.java instead.
A public class will need to be in a file named according to the class, so in this case it would be xSocketDataHandler.java.
Convention is also to name java classes starting with an upper-case letter, so it would be public class XSocketDataHandler and file XSocketDataHandler.java. This isn't required, though.